【发布时间】: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