2015-08-24 42 views
0

我正在使用登录进行应用程序开发。我想要做的是在登录后标签显示用户名。我无法通过标记目标来更改标签文本c

这里是我的代码:

#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 enter image description here

我使用的Xcode 7.0

+0

你应该使用塞格斯而非instantiateViewControllerWithIdentifier :.这是他们的目的。 –

+0

你为什么要将uiview转换为uilabel?不能像uilabel.tag一样直接访问标签标签吗? –

回答

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]; 
+0

尝试了两个但不起作用 – Jeroen

+0

您是否将IBoulet与故事板连接起来? –

2

U的2个ViewController不同实例,这意味着在改变第一个将是不可见的第二个中的UITextField。你需要有一个对象(我们称之为Model),它将保存数据和按需更改。

任何方式,改变你的架构有一个Model图层,它将保存您的数据。

如果你想在这里做一些特定的工作,但是练习方式不好,请在AppDelegate中创建NSString,并在更改时在其中设置数据。并根据需求从那里获取数据。

但同样, 改变你的架构有一个Model-View-Controller