Reputation: 472
I am working on app with a login. What i want to do is that after the login a label displays the username.
Here is my code:
#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UITextField *userNameTextField;
@property (strong, nonatomic) IBOutlet UITextField *passwordTextField;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)Login:(id)sender {
if ([_userNameTextField.text isEqualToString:@""] || [_passwordTextField.text isEqualToString:@""]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"alert" message:@"Please Fill all the field" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
return;
}
NSString *strURL = [NSString stringWithFormat:@"http://192.168.1.11:8888/swift/login.php?userName=%@&password=%@",_userNameTextField.text, _passwordTextField.text];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
if ([strResult isEqualToString:@"1"])
{
NSString *username = _userNameTextField.text;
UILabel *myLabel = (UILabel *)[self.view viewWithTag:100];
myLabel.text = [NSString stringWithFormat:@" Label %@",username];
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"logedIn"];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:vc animated:YES completion:NULL];
}
else if ([strResult isEqualToString:@"0"])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"alert" message:@"Wrong username / password" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
return;
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"alert" message:@"Server Error" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
return;
}
}
@end
Here is a image of my storyboard
In the image you see my first seen in the bottom left corner. the second screen is in the upper right corner. The both have the class ViewController
I am using xcode 7.0
Upvotes: 0
Views: 653
Reputation: 12951
U have 2 different instances of ViewController
which means the UITextField
that changes in the first one won't be visible to the second one.
U need to have an object (let's call it Model
) which will hold the data and change on demand.
Any way, change your architecture to have a Model
layer which will hold your data.
If u want to make things works specific here but in bad practice way, create NSString
in the AppDelegate
and set the data there when changed. And get the data from there on demand.
But again, change your architecture to have a Model-View-Controller
Upvotes: 2
Reputation: 2052
Set the property for your label in your .h and link it on your storyboard.
@property (nonatomic, strong) IBOutlet UILabel *label;
Then, before presenting your new view controller, you can just set the text for that label.
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"logedIn"];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
vc.label.text = [NSString stringWithFormat:@" Label %@",username];
[self presentViewController:vc animated:YES completion:NULL];
Upvotes: 0