简体   繁体   English

使用UI警报视图按钮关闭模态视图控制器

[英]Dismissing a Modal View Controller using the UI Alert View Button

I am new to Xcode somewhat. 我是Xcode的新手。 Right now I am creating a view controller (displaying modally) which displays a form for users to input information, and click "submit" to submit the information. 现在,我正在创建一个view controller (以模态显示),该view controller显示一个供用户输入信息的表单,然后单击“提交”以提交信息。

I created my IBAction , and implemented a UIAlerView that informs the user the information has been sent. 我创建了IBAction ,并实现了一个UIAlerView来通知用户信息已发送。 I would like the "Ok" button within my alert view to take them back to the original view controller. 我希望我的警报视图中的“确定”按钮将其带回原始视图控制器。 I set my Alert View delegate and implemented the following method in my .m file: 我设置了Alert View delegate并在.m文件中实现了以下方法:

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex

When I test it, nothing happens. 当我测试它时,什么也没有发生。 Can anyone tell me what I am doing wrong. 谁能告诉我我在做什么错。

You need to implement - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex delegate method --- 您需要实现- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex委托方法-

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //dissmiss here
    //Pre iOS 6.0
    [self dismissModalViewControllerAnimated:YES];

    //From iOS 5.0
    [self dismissViewControllerAnimated:YES completion:nil];
}

You can also check for the button which tapped, 您也可以检查点击的按钮,

if(buttonIndex != [alertView cancelButtonIndex]) {
    //do something
}

You need to implement the below mentioned delegate method of UIAlertView: 您需要实现以下提到的UIAlertView委托方法:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
    if ([buttonTitle isEqual:@"Ok"]) // Check for Ok button
    {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

SWIFT 5 SWIFT 5

I have been away form Xcode since swift 3 and needed to do this in my new project. 从Swift 3开始我就离开了Xcode,需要在我的新项目中做到这一点。 The below code worked for me (it presents an alert and upon closing the alert, the user is taken back to the previous view controller): 下面的代码为我工作(它显示一个警报,并在关闭警报后,将用户带回到上一个视图控制器):

func displayAlert() {
    let alert = UIAlertController(title: "Your Title",
                                  message: "Your Message",
                                  preferredStyle: .alert)
    let defaultButton = UIAlertAction(title: "OK",
                                      style: .default) {(_) in
                                        // your defaultButton action goes here
                                        self.dismiss(animated: true, completion: nil)
    }

    alert.addAction(defaultButton)
    present(alert, animated: true) {
        // completion goes here
    }
}

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

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