【问题标题】:How to properly dismiss UIAlertController both by button and tapping outside?如何通过按钮和点击外部正确关闭 UIAlertController?
【发布时间】:2016-10-19 10:12:27
【问题描述】:

我很清楚存在类似的问题,但我认为我未能找到解决此问题的适当解决方案。

如果您只看下面的代码,您可能会说它没有任何问题。但是,

如果我要在通过以下代码从情节提要调用的 UIViewController 上显示 UIAlertController,则使用警报控制器的按钮会首先关闭 UIAlertController,然后关闭我的 UIViewController。所以我导航回我的初始视图控制器。

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"MyViewController"];
[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:vc animated:NO completion:NULL];

这是 UIAlertController 的代码:

- (IBAction)myAlertAction:(id)sender {
    UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"Title"
                                                                        message:@"Lorem ipsum dolor sit amet"
                                                                 preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];

    [controller addAction:alertAction];

    [self presentViewController:controller animated:YES completion:^{
        controller.view.superview.userInteractionEnabled = YES;
        [controller.view.superview addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(alertControllerBackgroundTapped)]];
    }];

}

- (void)alertControllerBackgroundTapped
{
    [self dismissViewControllerAnimated: YES
                             completion: nil];
}

我该如何解决这个问题?

【问题讨论】:

  • UIAlertController *controller 全局声明这个然后执行你的操作
  • 你的代码是对的。您无需全局声明 UIAlertController *controller。您的代码中还有其他问题。
  • @balkaransingh 我不敢苟同。我刚刚在 xcode 中打开了另一个项目并再次测试它。结果是一样的
  • @TimurAykutYILDIRIM 我试试你的代码,它工作正常。
  • @balkaransingh 那么也许你也想试试这个? drive.google.com/open?id=0B-cDfWHidgvcb3pjdjNpZEpWZ1k

标签: ios objective-c uialertcontroller


【解决方案1】:
  UIAlertController *controller;


- (IBAction)Btn_alert:(id)sender {
    controller = [UIAlertController alertControllerWithTitle:@"Title"
                                                     message:@"Lorem ipsum dolor sit amet"
                                              preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
                              {
                                  //Do some thing here
                                  [controller dismissViewControllerAnimated:YES completion:nil];
                                   [self.presentedViewController presentingViewController];
                              }];

    [controller addAction:alertAction];

    [self presentViewController:controller animated:YES completion:^{
        controller.view.superview.userInteractionEnabled = YES;
        [controller.view.superview addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(alertControllerBackgroundTapped)]];
    }];
}

- (void)alertControllerBackgroundTapped
{
       [controller dismissViewControllerAnimated:YES completion:nil];
}

【讨论】:

  • 恐怕一切都没有改变
  • 我只是做了一个演示,在这个演示中它完全可以工作。你面临什么样的问题
  • 假设您有一个应用程序,它在 tableVC 上显示各种汽车名称以及每辆汽车的详细规格 VC。你调用 detailVC 就像我分享的第一个代码一样。 --- 现在在 detailVC 中有一个显示 UIAlertController 的按钮。使用此代码,如果我在 UIAlertController 之外触摸它很好,但是如果我通过单击警报控制器内部的按钮将其关闭,它会关闭警报和当前视图控制器。所以我导航回 tableVC
  • 我在 OS X 10.11.6 上使用 Xcode 版本 7.3 (7D175)
  • 它在最初几次可以正常工作。但随后它开始损坏并再次返回白色视图控制器 :D 但感谢您的帮助,我真的很感激
猜你喜欢
  • 1970-01-01
  • 2015-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-11
  • 2014-10-17
相关资源
最近更新 更多