0

Я использую приведенный ниже код для UIAlertController в моем проекте.Предупреждающее сообщение 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) 

Просьба направлять меня, как правильно использовать UIAlertController с помощью Objective C.

Спасибо, Abin Koshy Cheriyan

+0

Я создал проект GitHub для того же, который вы можете использовать, чтобы решить эту проблему: https://github.com/AgarwalMilan/MAAlertPresenter –

ответ

0

Я надеваю не знаю о вашей проблеме, но вы не должны этого делать

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

             }]; 

Это будет в любом случае отклонено на любом из ваших actions.

1

Да в соответствии с @Alexander вы не должны увольнять контроллер предупреждения, как это явно.

В соответствии с инженером-яблоком новое окно добавляется каждый раз, когда отображается UIAlertController, поэтому, когда мы отправляемся на его увольнение, окно все еще присутствует, хотя контроллер предупреждения исчезает.

Итак, есть два способа справиться с этим -

Way 1 - Нет явного не распускать ли явно не увольнять UIAlertController, пусть это будет сделано пользователем

Way 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: ДА завершение: ноль];

Даже вы можете оформить один из моего примера реализации here

Смежные вопросы