2015-08-24 40 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