IOS:具有两种不同委托方法的两个 UIAlert [英] IOS: two UIAlert with two different delegate methods
问题描述
我有一个 UIAlert
I have an UIAlert
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"ok"
message:@"Canc?"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:@"Annul", nil];
[alertView show];
[alertView release];
及其委托方法:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex == 0)//OK button pressed
{
//do something
}
else if(buttonIndex == 1)//Annul button pressed.
{
//do something
}
一切都好,但如果我有另一个alertview示例alertViewOne",我希望这个alertViewOne有它的委托方法,它不应该使用第一个alertview的委托方法;如何更改我的代码?
and it's all ok but if I have another alertview example "alertViewOne", I want that this alertViewOne have its delegate method and it shouldn't use delegate method of first alertview; how does change my code?
推荐答案
只需为每个 Alert 视图设置一个标签,然后检查哪个发送了消息.
Simply set a tag to each Alert view and check which one sent the messeg.
alertView.tag=0;
然后
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(alertView.tag==0){
if(buttonIndex == 0)//OK button pressed
{
//do something
}
else if(buttonIndex == 1)//Annul button pressed.
{
//do something
}
}else{
if(buttonIndex == 0)//OK button pressed
{
//do something
}
else if(buttonIndex == 1)//Annul button pressed.
{
//do something
}
}
更新使用块有更好的解决方案.
Update There is a better solution using blocks.
你可以看看这个项目,例如:UIAlertView-Blocks
You can look at this project for example: UIAlertView-Blocks
据我所知,iOS8 将带有带有块的本机警报.
And as far as I know iOS8 will come with native alerts with blocks.
这篇关于IOS:具有两种不同委托方法的两个 UIAlert的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!