Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用UI警报视图按钮解除模态视图控制器_Ios_Objective C_Xcode_Uialertview - Fatal编程技术网

Ios 使用UI警报视图按钮解除模态视图控制器

Ios 使用UI警报视图按钮解除模态视图控制器,ios,objective-c,xcode,uialertview,Ios,Objective C,Xcode,Uialertview,我对Xcode有点陌生。现在我正在创建一个视图控制器(以模块方式显示),它显示一个表单供用户输入信息,然后单击“提交”提交信息 我创建了我的iAction,并实现了一个UIAlerView,通知用户信息已发送。我希望警报视图中的“确定”按钮将它们带回原始视图控制器。我设置了我的警报视图委托,并在.m文件中实现了以下方法: - (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonI

我对Xcode有点陌生。现在我正在创建一个
视图控制器
(以模块方式显示),它显示一个表单供用户输入信息,然后单击“提交”提交信息

我创建了我的
iAction
,并实现了一个
UIAlerView
,通知用户信息已发送。我希望警报视图中的“确定”按钮将它们带回原始视图控制器。我设置了我的
警报视图委托
,并在.m文件中实现了以下方法:

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

当我测试它时,什么也没发生。谁能告诉我我做错了什么

您需要实现
-(void)alertView:(UIAlertView*)alertView单击按钮索引:(NSInteger)按钮索引
委托方法---

您还可以检查点击的按钮

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

您需要实现以下提到的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 3开始就离开了Xcode,需要在我的新项目中这样做。以下代码适用于我(它显示一个警报,关闭警报后,用户将返回到上一个视图控制器):


我必须实现这两种方法,还是只实现您发布的方法?dismissModalViewController不久前已贬值,您应该编辑您的答案以使用新方法。我还根据@rdelmar注释编辑了答案!如果它对你有帮助,请将其标记为已接受的答案,以便对其他人有用!
- (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];
    }
}
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
    }
}