由于iOS9中警报视图的弃用,我很难通过NSObject类显示警报
因为在UIAlertView中有一种显示警报的方法[objaler show];但在iOS 9中显示警报
我们必须使用presentViewController,以便任何人都可以告诉我如何在nsobject类中显示警报视图控制器。

我想在NSObject类中调用的代码如下:

   UIAlertController *alert= [UIAlertController
                               alertControllerWithTitle:@"Login"
                               message:@"Enter your credentials here"
                               preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction* ok = [UIAlertAction actionWithTitle:@"Done" style:UIAlertActionStyleDefault
                                               handler:^(UIAlertAction * action){
                                                   //Do Some action here
                                               }];
    UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault
                                                   handler:^(UIAlertAction * action) {
                                                      NSLog(@"cancel btn");
                                                       [alert dismissViewControllerAnimated:YES completion:nil];

                                                   }];
    [alert addAction:ok];
    [alert addAction:cancel];
   [self presentViewController:alert animated:YES completion:nil];

}

我的问题是我想在NSObject类中调用它。
怎么做?

最佳答案

试试这个 :

您的ObjectClassName.h类

#import <UIKit/UIKit.h>
+ (void)showAlertTitle:(NSString *)title withMessage:(NSString *)message onView:(UIViewController *)viewController;

您的ObjectClassName.m类
+ (void)showAlertTitle:(NSString *)title withMessage:(NSString *)message onView:(UIViewController *)viewController
{
    UIAlertController * alert=   [UIAlertController
                                  alertControllerWithTitle:title
                                  message:message
                                  preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction* okButton = [UIAlertAction
                                actionWithTitle:@"OK"
                                style:UIAlertActionStyleDefault
                                handler:^(UIAlertAction * action)
                                {
                                    //Handel your yes please button action here
                                    [alert dismissViewControllerAnimated:YES completion:nil];

                                }];

    [alert addAction:okButton];

   [viewController presentViewController:alert animated:YES completion:nil];


}

调用导入对象类时:
#import "ObjectClassName.h";
[ObjectClassName showAlertTitle:@"Alert" withMessage:@"Alert Message" onView:self];

关于ios - 如何在iOS9的NSObject类中调用UIAlertviewController?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33207705/

10-10 13:59