2015-10-30 72 views
-1

我有2個ViewControllers。登錄後,它將移動到下一個屏幕。然後會出現一個註銷按鈕。用戶按下注銷按鈕後,它應該返回到登錄屏幕,並顯示一個空的文本字段。如何註銷[iOS]

我該怎麼做?我嘗試了所有方法,但似乎並不奏效。

我的第一個視圖控制器:

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.sampleDictionary = @{@"username":@"alex", @"password":@"1234"}; 
} 

- (IBAction)loginTapped 
{ 
    if ([self.sampleDictionary[@"password"] isEqualToString:passwordField.text]) { 
     [self performSegueWithIdentifier:@"ss" sender:self]; 
    } else { 
     // Alert message 
     UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"wrong" message:@"Message" preferredStyle:UIAlertControllerStyleAlert]; 
     UIAlertAction *actionOk = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:nil]; 
     [alertController addAction:actionOk]; 
     [self presentViewController:alertController animated:YES completion:nil]; 
    } 
} 

@end 

這是我第二的ViewController:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.navigationItem.hidesBackButton = YES; 
} 

- (IBAction)logout:(id)sender { 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 
+0

那麼問題出在哪裏?它不回去?這些字段不是空的?或者是什麼... – emotality

+0

註銷不起作用 – jj1

+0

它怎麼不起作用?什麼是不發生什麼假設發生? – emotality

回答

0

在你的第二個視圖控制器:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    UIButton *logoutButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 100, 320, 50)]; 
    [logoutButton setTitle:@"Log Out" forState:UIControlStateNormal]; 
    [logoutButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
    [logoutButton setBackgroundColor:[UIColor blackColor]]; 
    [logoutButton addTarget:self action:@selector(logout) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:logoutButton]; 
} 

- (void)logout 
{ 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 
3

在你ViewController刪除文本在viewWillAppear中的用戶名和密碼字段。

- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 

    usernameField.text = @""; 
    passwordField.text = @""; 
} 
+0

註銷不起作用 – jj1

+0

如果我們做了@「」兩個文本字段。那麼我們將如何登錄用戶名/登錄密碼 – jj1

+0

註銷後,您的文本框將被上面的代碼清空。用戶再次輸入他們的憑證登錄。 – Vijay