Xcode 多视图;每个都有自己的按钮和动作
我正在Xcode 4.3中创建一个视图,我不确定如何指定多个UIAlertView,这些UIAlertView有自己的按钮和单独的操作。目前,“我的提醒”有自己的按钮,但操作相同。下面是我的代码Xcode 多视图;每个都有自己的按钮和动作,xcode,button,uialertview,ibaction,Xcode,Button,Uialertview,Ibaction,我正在Xcode 4.3中创建一个视图,我不确定如何指定多个UIAlertView,这些UIAlertView有自己的按钮和单独的操作。目前,“我的提醒”有自己的按钮,但操作相同。下面是我的代码 -(IBAction)altdev { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"titleGoesHere" m
-(IBAction)altdev {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"titleGoesHere"
message:@"messageGoesHere"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Continue", nil];
[alert show];
}
-(IBAction)donate {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"titleGoesHere"
message:@"messageGoesHere"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Continue", nil];
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.examplesite1.com"]];
}
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"examplesite2.com"]];
}
}
谢谢你的帮助 对于
UIView
(其中的UIAlertView
子类)有一个有用的属性标记
。您可以为每个警报视图设置不同的标记
更新:
#define TAG_DEV 1
#define TAG_DONATE 2
- (IBAction)altdev {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"titleGoesHere"
message:@"messageGoesHere"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Continue", nil];
alert.tag = TAG_DEV;
[alert show];
}
- (IBAction)donate {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"titleGoesHere"
message:@"messageGoesHere"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Continue", nil];
alert.tag = TAG_DONATE;
[alert show];
}
-(void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView.tag == TAG_DEV) { // handle the altdev
...
} else if (alertView.tag == TAG_DONATE){ // handle the donate
}
}
他是对的,但你需要补充一点:
-(void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView.tag == TAG_DEV && buttonIndex == 1) { // handle the altdev
...
} else if (alertView.tag == TAG_DONATE && buttonIndex == 1){ // handle the donate
}
}
如果buttonIndex==1,则使用第一个otherbutton。0将表示取消。但是只需对0执行任何操作,或者您可以执行此操作(检查标题名称),这只是另一个选项。。。注意标题相同的警报
-(IBAction)altdev {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"titleOneGoesHere"
message:@"messageGoesHere"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Continue", nil];
[alert show];
}
-(IBAction)donate {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"titleTwoGoesHere"
message:@"messageGoesHere"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Continue", nil];
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1)
{
if([[alertView title] isEqualToString:@"titleOneGoesHere"])
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.examplesite1.com"]];
}
else
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"examplesite2.com"]];
}
}
更容易更新
UIAlertView *alert = [[UIAlertView alloc] init...
alert.tag = 1;
UIAlertView *alert = [[UIAlertView alloc] init...
alert.tag = 2;
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(alertView.tag == 1) {
// first alert...
} else {
// sec alert...
}
}
全部完成 如果您发现很难使用委托方法以不同的方式标识警报视图,那么您也可以使用此类别类为每个警报视图使用完成块 例如
UIAlertView* alert1 = [[UIAlertView alloc] initWithTitle:@"AlertView+Block 1" message:@"WithBlocks" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
[alert1 showWithFinishBlock:^(UIAlertView *alertView, NSInteger buttonIndex){ //--AlertView1 Stuff here }];
UIAlertView* alert2 = [[UIAlertView alloc] initWithTitle:@"AlertView+Block 2" message:@"WithBlocks" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
[alert2 showWithFinishBlock:^(UIAlertView *alertView, NSInteger buttonIndex){ //--AlertView2 Stuff here }];
我希望这是一种比标记+委托方法更简单的方法。我仍然不太明白如何合并它。当我尝试将其放入时,无论我做什么,我都会使用未声明的标识符错误。如果您能回复我显示的代码和您的编辑,我将不胜感激。谢谢谢谢,真是太好了。我相信你可以告诉我,我还没有很好的经验,而且很高兴有像你这样的人来帮助我们。嗯,事实上,现在我遇到了一个问题,当用户点击“取消”按钮时,警报没有取消(只是继续对任一按钮执行操作)。有什么见解吗?我增加了另一个选项,但我喜欢这一个,所以增加了它。不要用0定义标记,因为这是所有对象上的默认标记(当然,除非您需要默认标记)。我想你现在已经解决了取消的问题,但这是因为你仍然需要测试按钮。