我正在使用登录功能在app上工作。我想要做的是登录后标签显示用户名。
这是我的代码:
#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
这是我的故事板的图像
在图片中,您会看到我第一次在左下角看到。第二个屏幕位于右上角。两者都有类ViewController
我正在使用xcode 7.0
答案 0 :(得分:2)
你有2个不同的 ViewController
个实例,这意味着第一个更改的UITextField
将不会显示在第二个实例中。
你需要有一个对象(让我们称之为 Model
),它将保存数据并按需更改。
无论如何,请将您的体系结构更改为拥有Model
图层,以保存您的数据。
如果你想让事情在这里具有特定的作用但是做法不好,请在NSString
中创建AppDelegate
并在更改后设置数据。并根据需要从那里获取数据。
但是, 将您的架构更改为Model-View-Controller
答案 1 :(得分:0)
在.h中设置标签的属性,并将其链接到故事板上。
@property (nonatomic, strong) IBOutlet UILabel *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];