0

我在我的项目中为 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。

谢谢,阿宾·科希·切里扬

4

2 回答 2

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];

[警报dismissViewControllerAnimated:是完成:无];

甚至您也可以在此处查看我的示例实现之一

于 2016-04-14T20:38:08.817 回答
0

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

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

                                       }];

无论如何,它都会在您的任何actions.

于 2015-11-15T11:34:53.187 回答