2009-08-10 97 views
-2

如何讓我的委託方法等待,直到用戶對alertview確認做出選擇?阻止執行,直到確認AlertView被用戶解僱,並返回結果

BOOL userChoice = FALSE; 

我有這樣的委託方法:

-(BOOL) returnUserChoiceYESorNO:(NSString*)message { //delegate method 

UIAlertView *msgBox = [[UIAlertView alloc] initWithTitle:@"Your Choice" message:message delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nil]; 
    [msgBox show]; 
    [msgBox release]; 

//// HOW CAN I MAKE IT WAIT HERE, UNTIL I RECEIVE USER Responce from ClickedButtonAtIndex? (without using while loop or infinite loops) 

return userChoice; 


} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 

if(buttonIndex==0) { 
userChoice = NO; 
else 
{ 
userChoice = YES; 
} 

} 

回答

0

不要等到出現的對話框返回。彈出警報,結束你的方法,然後在你做任何你需要做的事情之前等待警報返回,這會容易得多。

+0

這是一個DELEGATE方法。一旦呼叫,我們需要立即迴應。 – RealHIFIDude 2009-08-11 00:02:45

0

你不能沒有循環。如果你完全可以避免它,你不應該在那裏停下來。什麼叫你的方法?它可以重構爲稍後測試一個值,還是可以重寫一些東西,以便以後可以回答一個答案?

最糟糕的情況是,也許你可以繞開調用你的代碼,放置對話框,緩存值,然後創建任何條件將導致首先調用代碼。

這些GUI系統通常不會掛起,它們會返回並且大部分時間都沒有運行線程。如果你開始規避這種情況,你將會面臨各種線程和狀態問題。

0

創建用戶定義的方法一樣

-doSomething(){// 你執行你想任何行動準則。 }

當用戶點擊0索引時立即調用此方法。

(無效)alertView:(UIAlertView中*)alertView clickedButtonAtIndex:(NSInteger的)buttonIndex { 如果(buttonIndex == 0){ [自doSomething的]; else [self doOther]; }

無需創建方法

- (BOOL)returnUserChoiceYESorNO:(的NSString *)消息{

}

只需添加委託方法

-clickedButtonAtIndex

這是一種更簡單的實施方式。

相關問題