UIButton,UIAlertView设置动作

时间:2012-08-25 22:46:23

标签: ios uibutton uialertview

我有一个简单的问题,请帮助我,因为我真的卡住了。我需要的是,如果登录和密码不为空,则在按下登录按钮后切换到另一个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];这里也是通知 - “应用程序试图在目标上显示一个零模态视图控制器。”

1 个答案:

答案 0 :(得分:2)

根据您使用的是xib还是故事板,只保留下面解释的其中一行。

- (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];
    }
}