2011-12-22 26 views
2

我一直在谷歌上搜索這個問題現在幾乎一整天,沒有得到任何接近的解決方案,所以我想問一下你們.. :)UIAlertView中在appdelegate.m不起作用

我m工作在一個iOS應用程序,它應該通過WiFi連接到一個mbed,如果連接,並且如果沒有連接,給用戶一個對話框,然後給用戶重試的可能性。 我現在的問題是,我已經實施了在appdelegate.m中的連接方法,它是從這裏我想顯示警報..

警報它自我工作正常,但我有問題檢測何時按鈕是按下時,clickedButtonAtIndex不被調用。

我在appdelegate.h添加UIAlertViewDelegate,像這樣:

@interface AppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate, UIAlertViewDelegate> 

,並設置委託給自己,在alertview,像這樣:

alert_NOT = [[UIAlertView alloc] initWithTitle:@"Not connected!" message:message_to_user delegate:self cancelButtonTitle:@"Try again" otherButtonTitles: nil]; 
    [alert_NOT show]; 
    [alert_NOT release] 

和clickedButtonAtIndex貌似

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
NSLog(@"test"); 

}

所以我很想在alertview中按下按鈕時在日誌中看到「測試」一詞,但沒有任何反應。

更新: 試圖在我的「FirstViewController.m」實施它,有它的工作原理:如果可能的話秒,但我非常想擁有它的appdelegate.m ..

回答

1
@interface urAppDelegate : NSObject <UIApplicationDelegate,UIAlertViewDelegate> 

如果您合成的alert_not然後用自己使用它像這樣:

self.alert_NOT = [[UIAlertView alloc] initWithTitle:@"Not connected!" message:message_to_user delegate:self cancelButtonTitle:@"Try again" otherButtonTitles: nil]; 

[alert_NOT show]; 
[alert_NOT release]; 
+0

嗯,仍然沒有..委託在UIAlertView的聲明中設置爲self。 – 2011-12-22 09:00:48

+0

也沒有幫助..我設法解決我自己的問題,通過在我的視圖控制器中實現警報系統,它與我的代碼一起工作,然後從我的appdelegate.m中請求所需的方法。魅力。但非常感謝快速響應.. – 2011-12-22 10:21:33

0

您應該使用alertViewCancel方法這一點。

- (void)alertViewCancel:(UIAlertView *)alertView 
{ 
    NSLog(@"text"); 
} 
1

目前,我尋找到一個類似的實現,並想和大家分享的想法,我與你:也許使用NSNotification,當你委託的條件得到滿足火災,您也可以在VC中聽了( s),並在堆棧頂部以警報視圖進行適當處理。

0

定義如下:

#define appDelegate ((AppDelegate*)[UIApplication sharedApplication].delegate) 

和警報爲:

UIAlertView *alert_NOT = [[UIAlertView alloc] initWithTitle:@"Not connected!" message:message_to_user delegate:appDelegate cancelButtonTitle:@"Try again" otherButtonTitles: nil]; 
[alert_NOT show]; 

這裏設定委託作爲定義的關鍵字,即的appDelegate。

希望這會有所幫助。