简体   繁体   English

UIButton,UIAlertView设置动作

[英]UIButton,UIAlertView setting action

I have a simple question, please help me as I really stuck. 我有一个简单的问题,请帮助我,因为我真的卡住了。 What I need is to switch to another ViewController after login button is pressed if login and password are not empty 我需要的是,如果登录和密码不为空,则在按下登录按钮后切换到另一个ViewController

I have a simple login interface with username, password and login button. 我有一个简单的登录界面,包括用户名,密码和登录按钮。 UIAlertView is shown when password or login is empty, here's code: 当密码或登录为空时显示UIAlertView,这里是代码:

.m file .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]; 
    }
}

How to add action for the button to switch to other UIController? 如何为按钮添加动作以切换到其他UIController? This one is crushing all the time [self presentModalViewController: anotherUIViewController animated YES]; 这一个一直在粉碎[self presentModalViewController: anotherUIViewController animated YES]; Here's also notification -"Application tried to present a nil modal view controller on target ." 这里也是通知 - “应用程序试图在目标上呈现一个零模态视图控制器。”

Only keep one of the lines as explained below depending on if you are using xibs or storyboards. 根据您使用的是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];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM