0

我有一个简单的问题,请帮助我,因为我真的卡住了。如果登录名和密码不为空,我需要在按下登录按钮后切换到另一个 ViewController

我有一个简单的登录界面,带有用户名、密码和登录按钮。UIAlertView 在密码或登录为空时显示,代码如下:

.m 文件

- (IBAction)login:(id)sender {
    if (userName.text.length == 0 || password.text.length == 0){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Complete All Fields" message:@"You must complete all fields to login!" delegate:self cancelButtonTitle:@"Good" otherButtonTitles:nil, nil];
        [alert show]; 
    }
}

如何为按钮添加动作以切换到其他 UIController?这个一直很糟糕[self presentModalViewController: anotherUIViewController animated YES];这里还有一个通知 - “应用程序试图在目标上呈现一个 nil 模态视图控制器。”

4

1 回答 1

2

根据您使用的是 xibs 还是 storyboards,只保留下面解释的其中一行。

- (IBAction)login:(id)sender {
    if (userName.text.length == 0 || password.text.length == 0){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Complete All Fields" message:@"You must complete all fields to login!" delegate:self cancelButtonTitle:@"Good" otherButtonTitles:nil, nil];
        [alert show];
    }else{
        //if you are using xibs use this line
        UIViewController *anotherUIViewController = [[UIViewController alloc] initWithNibName:@"anotherUIViewController" bundle:[NSBundle mainBundle]];
        //
        //if you are using storyboards use this line
        UIViewController *anotherUIViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"someIdentifierForThisController"];
        //
        //then simply present the view controller
        [self presentViewController:anotherUIViewController animated:YES completion:nil];
    }
}
于 2012-08-26T05:45:44.457 回答