2015-10-19 49 views
2

由於在iOS9中棄用警報視圖,我很難通過NSObject類 顯示警報,因爲在UIAlertView中有一種方法可顯示警報[objaler show];但在iOS 9中顯示警報 我們必須使用presentViewController,以便任何人都可以告訴我如何在nsobject類中顯示警報視圖控制器。如何在iOS9的NSObject類中調用UIAlertviewController?

我想在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類。 如何做到這一點?

+1

爲什麼不嘗試使用NSNotification?在您的NSObject類上,發佈通知,然後在UIViewController類中添加一個觀察者,該類將調用選擇器來顯示您的UIAlertAction。 –

+0

@JohnRusselUsi好主意。 –

回答

0

這就是要點 - 蘋果告訴你,你可能不應該那樣做。

在這種情況下,您可以做的最好的做法是通過視圖控制器的層次結構(從UIWindow-rootViewController開始),並且至少將其呈現在頂層控制器上。

你一般可以做的最好的事情是直接在適當的視圖控制器中調用這樣的警報。

+0

我知道我們不能這樣做,而且你的答案定義我們必須去viewControllers層次結構,所以你的解決方案和我的解決方案是一樣的,我定義的代碼和你寫的理論。 – sohil

+0

這是一個常識,NSObject類沒有用戶界面,所以你不能做那件事。所以請先理解問題和答案。 – sohil

+0

我明白了這個問題。下面的答案不是解決方案,它只是通過方法分配視圖控制器的包裝,而不是直接分配和調用它。保存2行代碼,但找到合適的視圖控制器的問題仍然存在。 – michi

2

試試這個:

你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]; 
+0

好吧,OP可以簡單地推視圖控制器來提醒控制器,如果他有任何可用的。這並不能解決問題的關鍵。 – michi

0

你的戒備W是不是類方法

+(void)initialize but by the instance -(id)init method 

這就是爲什麼你的實例沒有得到通知

類方法「+(無效)初始化」被稱爲類時第一個理由叫加載。

實例方法「 - (id)init」的名稱以init開頭,在創建(實例化)對象時調用。

-(id)init { //alert view self = [super init]; return self; } 
+0

謝謝維傑。有用。 –

+0

Yankit快樂編碼 – vijay