2011-03-16 37 views
2

我正在使用驗證密鑰從使用Wi-Fi的服務器下載內容。如果許可證密鑰錯誤或無法使用Wi-Fi,我需要顯示UIAlert。我已經寫了顯示警報視圖的警報,但警報沒有被顯示......這是把我的腦袋裏流出的血液......任何人都可以幫忙......控制權正在通過這條線,但仍然是警報沒有被顯示。無法在我的應用程序中顯示UIAlertView

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{ 

NSFileManager *fileManager = [NSFileManager defaultManager]; 

NSString *documentsDirectory= [[[UIApplication sharedApplication] delegate] applicationDocumentsDirectory]; //[pathToStore objectAtIndex:0]; 

NSString *path = [documentsDirectory stringByAppendingFormat:@"packages"]; 

NSString *packagePath = [NSString stringWithFormat:@"%@/%@", path,isbnTemp]; 

[recievedData writeToFile:[documentsDirectory stringByAppendingPathComponent:@"file.zip"] atomically:YES]; 
NSString *zipPath=[documentsDirectory stringByAppendingPathComponent:@"file.zip"]; 

[fileManager createDirectoryAtPath:documentsDirectory withIntermediateDirectories:NO attributes:nil error:nil]; 

    ZipArchive *zipArchive = [[ZipArchive alloc]init]; 

if([zipArchive UnzipOpenFile:zipPath]){ 

    if([zipArchive UnzipFileTo:packagePath overWrite:YES]){ 

     [self loadContent]; 


    } 
    else{ 
     NSLog(@"Unable to UnArchieve the packages"); 
    } 


} 
else { 


    NSLog(@"Failure To Open Archive"); 
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Your ISBN and/or Licence Key are incorrect" message:Nil delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil]; 
    [alert show]; 
    [alert release];  
} 

}

+1

您是否嘗試過使用非零消息? – malinois 2011-03-16 14:47:42

+0

是的,我已經嘗試過,但我仍然運氣不佳.... – 2011-03-16 14:49:32

+0

日誌顯示? – malinois 2011-03-16 14:51:10

回答

5

您是否試圖在從主線程以外的線程調用的方法中顯示UIAlertView?例如,如果您試圖在異步回調中顯示UIAlertView,則它可以在單獨的線程上運行。

如果是這樣,您需要將顯示UIAlertView的代碼移動到單獨的選擇器,並使用performSelectorOnMainThread:方法之一在主線程上調用它。

例如,下面的方法添加到您的類:

-(void)showAlert { 
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Your ISBN and/or Licence Key are incorrect" message:Nil delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
} 

,然後更改的最後else子句在當前的代碼,以便它使用:

[self performSelectorOnMainThread:@selector(showAlert) withObject:nil waitUntilDone:NO]; 

更多請見NSObject class reference有關performSelectorOnMainThread:方法的信息。

+0

我剛纔試過它格雷格,但即使這是ont工作... – 2011-03-16 15:06:45

+0

請看我的更新問題格雷格...我已經把整個方法的問題 – 2011-03-16 15:36:34

+0

您更新的代碼仍然沒有使用performSelectorOnMainThread來顯示警報視圖。 – Greg 2011-03-17 17:23:46

0

你所創建的警報,你可以檢查在警報變量NULL指針後? 也許你需要指定一條消息?除此之外,我無法看到您發佈的代碼有任何問題。

+0

我無法理解這一點詹姆斯...我的意思是你可以請告訴我什麼是檢查NULL警報變量中的指針?你是在談論我傳遞給消息列的nil值? – 2011-03-16 14:58:17

+1

他意味着在創建警報的行後面設置一個斷點,當它到達時,將鼠標放在警報變量上並查看它是否指向nil。如果你不知道如何使用斷點,用NSLog以編程方式檢查它(alert?@「not nil」:@「nil」); – 2011-03-16 15:15:27

+0

ooh: - )...我做了它zaky .... itz不爲零..警報的值是0x65930cO .....任何線索? – 2011-03-16 15:30:22

相關問題