UIAlertController警告消息

时间:2015-10-30 04:34:14

标签: ios8 uialertcontroller presentviewcontroller uialertaction

我在我的项目中使用以下代码用于UIAlertController。

if([[[UIDevice currentDevice] systemVersion]floatValue] >= 8.0){

            UIAlertController * alert=   [UIAlertController
                                          alertControllerWithTitle:@"Input Error"
                                          message:@"Please enter a valid email."
                                          preferredStyle:UIAlertControllerStyleAlert];

            UIAlertAction* okAction = [UIAlertAction
                                       actionWithTitle:@"OK"
                                       style:UIAlertActionStyleDefault
                                       handler:^(UIAlertAction * action)
                                       {
                                           [alert dismissViewControllerAnimated:YES completion:nil];

                                       }];

            [alert addAction:okAction];

            [self presentViewController:alert animated:YES completion:nil];
        }
        else
        {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Input Error"
                                                            message:@"Please enter a valid email"
                                                           delegate:self
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil, nil];
        [alertView show];
        }

我收到以下警告信息:

Warning: Attempt to present <UIAlertController: 0x7f8da58df1f0>  on <MBComplaintsViewController: 0x7f8da36454d0> which is already presenting (null)

请指导我如何使用Objective C正确使用UIAlertController。

谢谢, Abin Koshy Cheriyan

2 个答案:

答案 0 :(得分:1)

是的,根据@Alexander你不应该明确地解雇警报控制器。

根据苹果工程师的说法,每次显示UIAlertController时都会添加一个新窗口,所以当我们解除它时,虽然警报控制器消失,但窗口仍然存在。

所以有两种方法可以解决这个问题 -

方式1 - 没有明确解雇 不要明确地解除UIAlertController,让它由用户完成

方式2 - 使用您自己的窗口 只需在UIAertController上创建一个类别 这是示例代码 - 的·H

#import <UIKit/UIKit.h>

@interface UIAlertController (MyAdditions)

@property(nonatomic,strong) UIWindow *alertWindow;

-(void)show;

@end

.m

#import "UIAlertController+MyAdditions.h"
#import <objc/runtime.h>

@implementation UIAlertController (MyAdditions)

@dynamic alertWindow;

- (void)setAlertWindow:(UIWindow *)alertWindow {
    objc_setAssociatedObject(self, @selector(alertWindow), alertWindow, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIWindow *)alertWindow {
    return objc_getAssociatedObject(self, @selector(alertWindow));
}

- (void)show {
    self.alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.alertWindow.rootViewController = [[UIViewController alloc] init];

    // window level = topmost + 1
    UIWindow *topWindow = [UIApplication sharedApplication].windows.lastObject;
    self.alertWindow.windowLevel = topWindow.windowLevel + 1;

    [self.alertWindow makeKeyAndVisible];
    [self.alertWindow.rootViewController presentViewController:self animated:YES completion:nil];
}

-(void)hide {
    self.alertWindow.hidden = YES;
    self.alertWindow = nil;
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];

    // just to ensure the window gets desroyed
    self.alertWindow.hidden = YES;
    self.alertWindow = nil;
}

显示警报控制器

UIAlertCntroller *alert = ## initialisation##;
// will show the alert
[alert show];

//to dismiss 
[alert hide];

[alert dismissViewControllerAnimated:YES completion:nil];

即使你可以查看我的一个示例实现here

答案 1 :(得分:0)

我不知道你的问题,但你不应该这样做

handler:^(UIAlertAction * action)
                                       {
                                           [alert dismissViewControllerAnimated:YES completion:nil];

                                       }];

无论如何,你的任何actions都会被解雇。