2010-02-26 33 views
1

我有一些代碼,當用戶點擊遊戲結束時,它會提示他們,如果想再次發揮:UIAlertView中和detemining點擊什麼

-(void)showAlert 
{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@" B U S T E D ! " 
                message:@"Sorry, you busted!\n\nWant to try your luck 1 More Time! ?" 
                delegate:self 
              cancelButtonTitle:@"Cancel" 
              otherButtonTitles:@"New Game", nil]; 
    [alert show]; 
    [alert release]; 
} 


- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 
    // the user clicked one of the OK/Cancel buttons 
    if (buttonIndex == 0) 
    { 
     //here is where we can close it 
    } 
    if (buttonIndex == 1) 
    { 
     [self createNewGame]; 
    } 
} 

現在我想也做一次檢查時用戶首先啓動應用程序以查看是否存在先前的遊戲文件,如果是,詢問他們是否想要繼續。我知道我可以通過:

-(void)priorGameExists 
{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@" Previous Game Exists ! " 
                message:@"A previous game currently exists. Would you like to resume that game?" 
                delegate:self 
              cancelButtonTitle:@"Cancel" 
              otherButtonTitles:@"Resumse", nil]; 
    [alert show]; 
    [alert release]; 
} 

但是,我該如何去一個新的「自定義」clickedButtonAtIndex?我是否認爲它與設置不同的代表有關?如果是這樣,我該怎麼做?

+0

的答案似乎集中在兩個UIAlertViews,這是解決你的問題的一種方法之間的區別,所以我投票關閉此爲重複。 – 2010-02-26 16:28:16

回答

-1

clickedButtonAtIndex方法測試傳入alertview的稱號。

if ([actionSheet.title isEqualToString:@" B U S T E D ! "]) { 
    // do some busted stuff here 
else if ([actionSheet.title isEqualToString:@" Previous Game Exists ! "]) { 
    // do some previous game stuff here 
} 

你可能想使用靜態字符串,那麼你只需要在你的代碼,一個地方的字符串來設置這些頭銜,但是這基本上是你會怎麼做。

+0

呃。什麼與評論downvotes?特別是因爲這個答案與Can Berk's幾乎相同。沒有優雅,但這不是一個downvote的理由。 – kubi 2010-02-26 16:56:09

0

一種解決方法是聲明一些UIAlertView中私有類的實例那樣:在你看來控制器

@interface myViewControllerInterface : UIViewController { 
@private 
    UIAlertView *newGameAlert; 
    UIAlertView *resumeGameAlert; 
} 

然後你可以用它們創建alertViews:

-(void)showAlert { 
newGameAlert= [[UIAlertView alloc] initWithTitle:@" B U S T E D ! " 
     message:@"Sorry, you busted!\n\nWant to try your luck 1 More Time! ?" 
      delegate:self cancelButtonTitle:@"Cancel" 
     otherButtonTitles:@"New Game", nil]; 
[newGameAlert show]; 
[newGameAlert autorelease]; 
} 

-(void)priorGameExists { 
resumeGameAlert = [[UIAlertView alloc] initWithTitle:@" Previous Game Exists ! " 
     message:@"A previous game currently exists. Would you like to resume that game?" 
      delegate:self cancelButtonTitle:@"Cancel" 
     otherButtonTitles:@"Resumse", nil]; 
[resumeGameAlert show]; 
[resumeGameAlert autorelease]; 
} 

而完成,你可以使用指針在每個警報視圖之間區別:

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (actionSheet == newGameAlert) { 
    //do something 
    } else if (actionSheet == resumeGameAlert) { 
     //do something 
    } 
} 
+0

如果你這樣做,你應該設置newGameAlert和resumeGameAlert在委託方法零,否則,你將有一個指針公佈的對象,這是潛在的危險。此外,Can BerkGüder的解決方案非常乾淨,不需要任何ivars,而且非常漂亮。 – Costique 2010-02-26 16:35:18

+0

是的,我同意這一點,但我不知道這一點。直到現在,我都是這樣做的,但現在我發現了這種新技術,我將使用它。我們每天都在這裏學習新東西。 =) – 2010-02-26 16:46:04

0

你可以使用一個不同的委託,但一個簡單的方法是將tag屬性設置爲唯一值。如果tag是,說,10你會知道它是從原始的警告,如果這是20這將是從priorGameExits問題。 (你應該使用當然是常數。)

相關問題