Iphone 在objective c中为不同的UIAlert分离事件处理程序
好的,假设我有2个uialerts,并且在这两种情况下都将委托设置为self,第一个uialerts包含(ok&download)按钮第二个包含(cancel&upload)按钮现在我们需要单独的事件处理程序知道吗 要在UIView中处理多个UIAlertView,必须为每个UIView设置唯一的标记Iphone 在objective c中为不同的UIAlert分离事件处理程序,iphone,objective-c,xcode,Iphone,Objective C,Xcode,好的,假设我有2个uialerts,并且在这两种情况下都将委托设置为self,第一个uialerts包含(ok&download)按钮第二个包含(cancel&upload)按钮现在我们需要单独的事件处理程序知道吗 要在UIView中处理多个UIAlertView,必须为每个UIView设置唯一的标记 UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"title" message:@"szMsg" delegate:self
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"title" message:@"szMsg" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:@"download"];
[alert show];
[alert release];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
//Code for OK button
}
if (buttonIndex == 1)
{
//Code for download button
}
}
在从委托方法获取响应的同时,使用唯一的标记管理每个方法
alert.tag = 123;
尝试为两个不同的
UIAlertView
实例设置tag
属性,然后在回调中再次检查这些标记,并在那里执行其余操作,例如:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(alertView.tag == 123)
{
if (buttonIndex == 0)
{
//Code for OK button
}
else if (buttonIndex == 1)
{
//Code for download button
}
}
else if(alertView.tag == 456)
{
// code to manage another alertview response.
}
}
这是委托回调
UIAlertView *alertDownload = [[UIAlertView alloc]initWithTitle:@"title" message:@"szMsg" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:@"download"];
alertDownload.tag = 1;
[alertDownload show];
[alertDownload release];
UIAlertView *alertUpload = [[UIAlertView alloc]initWithTitle:@"title" message:@"szMsg" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"upload"];
alertUpload.tag = 2;
[alertUpload show];
[alertUpload release];
如果有很多警报视图或委托方法很复杂,则可以创建一个控制器(一个简单的
NSObject
子类)来管理每个警报视图,例如downloadConfirmationalController
。然后,您的主控制器可以存储对子控制器的引用。但请使用枚举或#定义标记值。我讨厌看到各种各样的.tag=1234
和带有tag:1234代码的视图。使用命名常量<代码>如果(alertView.tag==kDownloadAlertView){…}更具可读性!神奇的数字是个坏主意。不是123
,而是为每个警报视图声明一个带有值的enum
。例如,enum ClassNameAlertViews{ClassNameConfirmCancelDownloadAlert=123,ClassNameConfirmOtherUserAction,您可以通过AddingUndoSupportAlert=234}删除它们@迈克·韦勒和本尼迪克特·科恩:当然可以,先生。感谢您的反馈。只需显式地将第一个枚举值设置为1,然后自动分配其余的枚举值。我不知道这件“1234”的事情是从哪里来的,但似乎每个人都这么做了。
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(alertView.tag == 1) {
//Here you do your stuff for Download
}
if(alertView.tag == 2) {
//Here you do stuff for Upload
}
}
my_Alert = [[UIAlertView alloc]initWithTitle:@"Hi" message:@"Hello" delegate:self
cancelButtonTitle:@"OK" otherButtonTitles:@"Cancel",nil];
my_Alert.frame = CGRectMake(462, 359, 400, 50);
my_Alert.tag = 1;
my_Alert = [[UIAlertView alloc]initWithTitle:@"Hi" message:@"Hello" delegate:self
cancelButtonTitle:@"OK" otherButtonTitles:@"Cancel",nil];
my_Alert.frame = CGRectMake(462, 359, 400, 50);
my_Alert.tag = 2;
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex==0 && my_Alert.tag == 1)
{
NSLog(@"Perform action on button touch of index 0 of First Alert");
}
else
{
NSLog(@"Perform action on button touch of index 1 of First Alert");
}
if (buttonIndex==0 && my_Alert.tag == 2)
{
NSLog(@"Perform action on button touch of index 0 of Second Alert");
}
else
{
NSLog(@"Perform action on button touch of index 1 of Second Alert");
}
}