2013-01-02 75 views
3

我已經在處理所有服務器請求的方法中實現了Reachability功能。我可以通過NSLog看到該函數完美工作。但是在方法中從來沒有「暫停」,這意味着我不能在不崩潰程序的情況下使用UIAlertView。爲什麼在顯示UIAlertView時應用程序崩潰?

我可能在這個可以去完全錯誤的方式,但我無法找到任何東西...

有誰知道如何獲得通知以某種方式表明的想法?

在此先感謝

CODE:

-(id) getJson:(NSString *)stringurl{ 
Reachability * reach = [Reachability reachabilityWithHostname:@"www.google.com"]; 

NSLog(@"reached %d", reach.isReachable); 

if (reach.isReachable == NO) { 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Passwords don't match." 
    message:@"The passwords did not match. Please try again." 
    delegate:nil 
    cancelButtonTitle:@"OK" 
    otherButtonTitles:nil]; 
    [alert show]; 

}else{ 
    id x =[self getJsonFromHttp:stringurl]; 
    return x; 
} 
return nil; 
} 
+0

您能否至少發佈標題引用的「函數」的全部內容?希望看到更多的代碼 - 也許更清晰的描述。 – sean

+0

完成。雖然我不認爲這會對額外的部分產生太大的幫助......但這個想法是讓我能夠在沒有程序崩潰的情況下顯示UIAlertView。有沒有辦法「暫停」應用程序,直到警報框被解除?或者我應該對問題採取完全不同的方法? – Tom

+0

你的代碼是否用那個空的return來編譯? [alert show]後的聲明?應該返回一些東西,因爲編譯器正在尋找你返回一個(id)。 – sean

回答

2

移動討論到聊天后,我們發現您的UIAlertView中正在從後臺線程調用。切勿在後臺線程中更新與更新UI(用戶界面)相關的任何內容。 UIAlertView通過添加一個彈出式對話框來更新用戶界面,因此它應該在主線程上完成。通過進行以下更改來修復:

// (1) Create a new method in your .m/.h and move your UIAlertView code to it 
-(void)showMyAlert{ 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Passwords don't match." 
          message:@"The passwords did not match. Please try again." 
          delegate:nil 
           cancelButtonTitle:@"OK" 
          otherButtonTitles:nil]; 
    [alert show]; 

} 

// (2) In -(id)getJson replace your original UI-related code with a call to your new method 
[self performSelectorOnMainThread:@selector(showMyAlert) 
          withObject:nil 
          waitUntilDone:YES]; 
相關問題