2013-11-23 1273 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