2011-09-20 107 views
3

我想知道下面的代碼是否可以。我想從「timedAlert」方法中2秒後自動關閉alertView(並且沒有alertView中的任何按鈕)。UIAlertView沒有任何按鈕

//this is in another method 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Login successful." delegate:self cancelButtonTitle:nil otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
    [self timedAlert]; 
} 

-(void)timedAlert 
{ 
    [self performSelector:@selector(dismissAlert:) withObject:alert afterDelay:2]; 
} 

-(void)dismissAlert:(UIAlertView *) alertView 
{ 
    [alertView dismissWithClickedButtonIndex:nil animated:YES]; 
} 

如果alertView的cancelButton被設置爲 「無」,將如何在 「[alertView dismissWithClickedButtonIndex:0動畫:是];」事情工作?我試圖使cancelButton「無」和它的工作,但不能弄清楚如何....

P.S:我叫timedAlert方法從另一個

任何幫助表示讚賞!謝謝!

+2

是的,你的代碼是好的。你可以在這裏閱讀更詳細的解釋:http://iphonedevelopertips.com/user-interface/uialertview-without-buttons-please-wait-dialog.html – adamsiton

+0

謝謝!但我仍然得到disviewWithClickedBittonIndex如何在alertView中沒有按鈕時正常工作。特別是爲什麼按鈕索引被接受...請幫助! – Balaram

+0

我不太明白你的問題。在dismissWithClickedBittonIndex方法中,按鈕索引是可選的。如果你通過零(或0),比警報簡單地解僱。 – adamsiton

回答

1

你的代碼應該可以工作,而且你應該沒有問題。我已經在我之前的一個應用程序中完成了這一點。該按鈕不顯示,因爲標題爲零,但我認爲該按鈕的實例仍然存在。在關閉警報之前放置一個斷點並查看警報變量,然後檢查是否有按鈕數組或其他東西,這應該告訴你這是如何工作的。

+0

有一個按鈕可變數組,但它顯示「0對象」... – Balaram

+0

theres也NSInteger值讀取-1取消按鈕,默認按鈕,第一個其他按鈕..這就是對的?那是因爲dismissWithClickedButtonIndex接受索引的零值????? – Balaram

+0

我還看到一個「_dismissButtonIndex」字段,它讀取0值!我希望這一切都說!我對麼? – Balaram

4

首先我要說,如果你處理這與自定義視圖可能會更好,但認爲問題看起來是與

[alert release]; 

您正在釋放的對象,你用它做前(我很驚訝它不會崩潰)。

做這樣的事情

// other code 
alert = [[UIAlertView alloc] initWithTitle:nil message:@"Login successful." delegate:self cancelButtonTitle:nil otherButtonTitles:nil]; 
    [alert show]; 
    [self performSelector:@selector(dismissAlert:) withObject:alert afterDelay:3.0f]; 
} 

-(void)dismissAlert:(UIAlertView *) alertView 
{ 
    [alertView dismissWithClickedButtonIndex:nil animated:YES]; 
    [alertView release]; 
} 
+0

感謝您的建議!這看起來更可以接受! – Balaram

+0

它不會崩潰,因爲[alert show]保留了對象,'alert'變量仍然指向它。這個答案中的修改代碼是可以的,但[alert release]可以留在[self performSelector ....]之後的代碼中,並從dismissAlert視圖中移除。或者現在轉移到ARC以擺脫所有這些混亂。 –