2012-07-17 40 views
0

我最近將MDProgressHUD添加到了我的應用程序中,並按照ReadMe文檔中的說明建議我在主隊列上設置HUD,然後在新線程上執行其他任務,這是我如何實現的它。這一切都可以在應用程序的初始啓動時正常工作,但如果您選擇手機上的主頁按鈕,以便將應用程序置於後臺,然後選擇應用程序的圖標將其恢復到前臺,則應用程序將崩潰。進入前臺時應用程序崩潰

我已經實現了代碼如下。當用戶選擇「確定」按鈕時,應用程序將通過調用Web服務(NSURLConnection)(即authenticateUser)來驗證其登錄標識和密碼。

- (IBAction)Ok:(id)sender { 

    [self.txtPassword resignFirstResponder]; 

    [MBProgressHUD showHUDAddedTo:self.view animated:YES]; 
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.01 * NSEC_PER_SEC); 
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 
     [self authenticateUser]; 
     [MBProgressHUD hideHUDForView:self.view animated:YES]; 
    }); 

} 

- (void)authenticateUser { 

    self.loginEmailAddress = self.txtEmailAddress.text; 
    self.loginPassword = self.txtPassword.text; 

    if ([self.loginEmailAddress isEqualToString:@""] || [self.loginPassword isEqualToString:@""]) { 
    self.lblErrMsg.text = @"Invalid login details. Please try again."; 
    self.lblErrMsg.hidden = NO; 
    [MBProgressHUD hideHUDForView:self.view animated:YES]; 
    return; 
} 


    NSString *myRequestString = [NSString stringWithFormat:@"org_id=%@&login_id=%@&pword=%@", signedUpOrgId, self.loginEmailAddress, self.loginPassword]; 
    NSString *myEncodedRequestString = [myRequestString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; 

    NSData *myRequestData = [myEncodedRequestString dataUsingEncoding:NSUTF8StringEncoding]; 

    NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:serviceURL 
                 cachePolicy:NSURLRequestUseProtocolCachePolicy 
                timeoutInterval:60.0]; 


    [theRequest setHTTPMethod: @"POST"]; 
    [theRequest setHTTPBody: myRequestData]; 

    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 

    if (theConnection) { 
     receivedData = [NSMutableData data]; 

    } else { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorry! Can't connect." message:@"Sorry, but there seems to be a problem connecting to the internet at the moment. Please try again or wait until you have better reception for a data connection." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; 
     [alert show]; 
    } 
} 

日誌:

2012-07-17 23:40:33.699 AppName[6155:707] -[ContactListViewController authenticateUser]: unrecognized selector sent to instance 0x2616e0 
2012-07-17 23:40:33.704 AppName[6155:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ContactListViewController authenticateUser]: unrecognized selector sent to instance 0x2616e0' 
*** First throw call stack: 
(0x3146588f 0x377a3259 0x31468a9b 0x31467915 0x313c2650 0x31d78933 0x31439a33 0x31439699 0x3143826f 0x313bb4a5 0x313bb36d 0x33435439 0x30b1ecd5 0xe13c7 0xe136c) 
terminate called throwing an exception 

我想不通,爲什麼應用程序崩潰的第二次。任何援助將不勝感激。

+1

實際上是在名爲'ContactListViewController'的類中爲'authenticateUser'發佈的代碼嗎? (如果是的話,試着用Instruments來運行,使用檢查殭屍的設置。) – 2012-07-17 14:05:12

+0

你是否在'.h'文件中聲明瞭你的方法?因爲您似乎在嘗試在實現之前調用該方法。測試它的一個快速方法是移動上面的方法' - (IBAction)好:(id)sender' – Alladinian 2012-07-17 14:10:30

+1

@Alladinian這是一個Objective-C運行時錯誤,其中.h文件沒有意義(您不需要聲明一個方法可供運行時試圖找到並運行它,而運行時不會使用頭信息來進行查找(只有編譯器纔會這樣做)。 – 2012-07-17 14:39:20

回答

0

當在一個塊中引用self時,建議通過一個「弱」變量間接進行(否則您可能永遠不會正確解除分配)。

__weak ContactListViewController * weakself = self; dispatch_after(popTime,dispatch_get_main_queue(),^(void){weakself authenticateUser]; [MBProgressHUD hideHUDForView:weakself.view animated:YES]; });

這是因爲對你的引用綁定到該塊;它有一個重要的含義:你是否當然,你掛在這個塊足夠長的時間足以讓你有一個很好的參考?您可能需要考慮這一點,並確保當被稱爲「屬於」其他人足夠長時間的對象爲self時仍然是該塊的有效實例。

相關問題