2011-12-26 75 views
7

我有一個可怕的時間讓UIAlertView在我的自定義NSObject類中工作。在我做的研究中,它應該是可能的,但這是我遇到的。在NSObject中使用UIAlertView

首先,這裏是我的代碼:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    NSLog(@"clickedButtonAtIndex: %d", buttonIndex); 
} 

-(void)testAlertView { 
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"List Contains Items" 
        message:@"List contains items. Remove all items & delete?" 
        delegate:self 
        cancelButtonTitle:@"No" 
        otherButtonTitles:@"Yes", nil]; 
    [alertView show]; 
} 

如果我設置爲自己的代碼,只要我輕按一鍵崩潰的委託。如果我將它設置爲零,clickedButtonAtIndex永遠不會被調用。我已經嘗試過,沒有使用<UIAlertViewDelegate>

我知道有人會問'你爲什麼要在NSObject中而不是在你的UIViewController中'。主要是因爲我想分開這個代碼,所以我可以在我的應用程序的多個地方使用它。但也是因爲這是邏輯的一大塊很小的一部分,它有自己的意義。

任何想法我做錯了什麼?

感謝, 豐富

+0

此代碼*應*工作,其實。當代理被設置並且警報崩潰時,它究竟在哪裏崩潰並且存在堆棧跟蹤? – 2011-12-26 20:19:50

+1

當您點擊按鈕時,您的對象是否已被釋放? (對象通常不會保留他們的代表) – 2011-12-26 20:26:41

+0

這只是典型的。我花了幾個小時盯着這個,研究並嘗試了一件又一件沒有成功的事情,所以我決定尋求幫助。不到一個多小時後,我終於找到了答案。它與我在調用視圖控制器中創建自定義對象的方式有關,所以當我按下按鈕時它已經釋放,就像Frederick所建議的那樣。感謝輸入的人! – rdfrahm 2011-12-26 21:10:41

回答

8

我使用ARC有同樣的問題。問題的根源是一樣的。我解決了這個問題,只要調用對象(在我的情況下是UIVIewCOntroller)存在,將我的自定義NSObject放入一個「strong」屬性中,以確保該對象存在,所以當我調用alert視圖的委託時,我仍然有我的自定義對象周圍和委託方法正常工作。

+0

你先生今天可能剛剛救了我幾個小時! – ebi 2015-03-24 14:39:15

1

添加NSObject的強勁性能:

#import "Logout.h" // is NSObject 
. 
. 
. 
@property (nonatomic, strong) Logout *logout; 

然後你會得到所謂的在NSObject的的delegatemethods。

不要忘了登記委託的UIAlertView中:

@interface Logout() <UIAlertViewDelegate> 

,並在你的方法:

UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"title" 
message:@"message" delegate:self cancelButtonTitle:@"cancel" 
otherButtonTitles:@"ok", nil];    

[a show]; 
0

如何呈現警報視圖使用UIAlertController當你沒有視圖控制器。 Detail description

是的,你只能在UIViewController類中使用UIAlertController。那麼我們怎麼在NSObject類中做到這一點。如果你看到上面給出的描述鏈接,你將會得到答案。總結上面的描述:在當前窗口上方創建一個新窗口。這個新窗口將成爲我們的viewController,在其中顯示警報。所以使用這個viewController你可以調用方法[presentViewController: animated: completion:]

答:

dispatch_async(dispatch_get_main_queue(), ^{ 

        UIWindow* window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; 

        window.rootViewController = [UIViewController new]; 
        window.windowLevel = UIWindowLevelAlert + 1; 
        NSString *[email protected]「Your mssg"; 
        UIAlertController* alertCtrl = [UIAlertController alertControllerWithTitle:@「Title" message:msg preferredStyle:UIAlertControllerStyleAlert]; 

        [alertCtrl addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Yes",@"Generic confirm") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { 

         // do your stuff 
         // very important to hide the window afterwards.      
         window.hidden = YES; 

        }]]; 

        UIAlertAction *cancelAction= [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 

         window.hidden = YES; 

        }]; 

        [alertCtrl addAction:cancelAction]; 

       //http://stackoverflow.com/questions/25260290/makekeywindow-vs-makekeyandvisible 

        [window makeKeyAndVisible]; //The makeKeyAndVisible message makes a window key, and moves it to be in front of any other windows on its level 
        [window.rootViewController presentViewController:alertCtrl animated:YES completion:nil]; 

       });