2013-11-23 1298 views
-1

我有兩個樣品按鈕,並與他們每個人的,兩個不同的alertView應單獨彈出。第一個效果不錯,但是打到第二個,不知道爲什麼兩個alertView一個接一個彈出來。AlertView彈出兩次單按鈕

下面是代碼:

- (IBAction)AlertViewButtonTwo:(id)sender 
{ 
UIAlertView *myAlertOne = [[UIAlertView alloc] initWithTitle:@"AlertView" message:@"AlertViewOne" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"GoToYahoo", @"GoToYoutube", @"GoToFacebook", nil]; 
myAlertOne.tag = 1; 

[myAlertOne show]; 
} 

- (IBAction)AlertViewButtonThree:(id)sender 
{ 
UIAlertView *myAlertTwo = [[UIAlertView alloc] initWithTitle:@"AlertView" message:@"AlertViewTwo" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"GoToDailyStar", @"GoToProthomAlo", @"GoToNewAgeBD", nil]; 
myAlertTwo.tag = 2; 

[myAlertTwo show]; 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
if (alertView.tag == 1) 
{ 
    if (buttonIndex == 0) 
    { 
     // this is the cancel button 
    } 
    else if (buttonIndex == 1) 
    { 
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.yahoo.com/"]]; 
    } 
    else if (buttonIndex == 2) 
    { 
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.youtube.com/"]]; 
    } 
    else if (buttonIndex == 3) 
    { 
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.facebook.com/"]]; 
    } 
} 
else if (alertView.tag == 2) 
{ 
    if (buttonIndex == 0) 
    { 
     // this is the cancel button 
    } 
    else if (buttonIndex == 1) 
    { 
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.thedailystar.net/"]]; 
    } 
    else if (buttonIndex == 2) 
    { 
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.prothom-alo.com/"]]; 
    } 
    else if (buttonIndex == 3) 
    { 
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.newagebd.com/"]]; 
    } 
} 
} 

我是新來的iOS中。如果有人告訴我這個缺點,那將是非常可觀的。 在此先感謝。

+0

你是如何創建按鈕並設置目標的? – Wain

+0

對不起,我沒聽懂你...北斗星 – Tulon

回答

1

其中一個按鈕可能具有多個連接到它的目標/操作對,所以當您期望它只調用一個時,它會調用這兩種方法。檢查XIB/Storyboard中的連接,並刪除不應該在那裏的任何內容(可能是由於複製或放錯地方的拖動)。

+0

感謝的人,我只是在故事板製作的時間將複製「AlertViewButtonTwo」按鈕,在「AlertViewButtonThree」按鈕,而這已經是曾在「類的動作。 h「文件。我刪除以前的按鈕(「AlertViewButtonThree」),創造了一個新的和現在它工作正常。感謝您的評論。 @Wain – Tulon