Iphone 不同的警报视图和viewcontroller
在同一个视图控制器中,我必须通过警报按钮触发的不同操作显示不同的警报(此视图控制器是警报的代理) 有没有办法重用警报代码init/show/releaseIphone 不同的警报视图和viewcontroller,iphone,objective-c,uiviewcontroller,uialertview,code-reuse,Iphone,Objective C,Uiviewcontroller,Uialertview,Code Reuse,在同一个视图控制器中,我必须通过警报按钮触发的不同操作显示不同的警报(此视图控制器是警报的代理) 有没有办法重用警报代码init/show/release - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 我需要警报能够区分。您可以在此处获得警报视图 - (void)alertView:(UIAlertView *)alertView clickedButtonA
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
我需要警报能够区分。您可以在此处获得警报视图
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if([alertView isEqualTo:yourAlertView]) // you can try isEqual:
{
//Do something
}
//Another option is set tags to alertviews and check these tags
// if(alertView.tag == yourAlertView.tag)
//{
//Do something
//}
}
正是7KV7所说的。您需要标记警报视图,并在事件处理程序中检查发送者的标记是什么。通过这种方式,您可以在同一事件处理程序(ClickedButtonIndex)中为不同的警报视图创建不同的操作。您可以定义一组常量来表示正在管理的每种不同类型的警报视图。例如:
enum {
MyFirstTypeOfWarning,
MySecondTypeOfWarning
};
typedef NSInteger SPAlertViewIdentifier;
然后,当您发现自己需要显示UIAlertView时,调用一个方法,该方法包装init/show代码并设置UIAlertView的标记:
- (void)initializeAndPresentUIAlertViewForWarningType:(SPAlertViewIdentifier)tag {
// Standard alloc/init stuff
[alertView setTag:tag];
[alertView show];
}
然后,在alertView:ClickedButtonIndex:中,您可以检查传入的警报视图的标记并做出相应的响应
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if ([alertView tag] == MyFirstTypeOfWarning) {
// Process button index for first type of alert.
} ...
}
因此,如果我将alert init/show/release代码放在一个方法中,我可以向它传递一个标记(我想是一个NSString*),然后检查方法委托中的标记是否相等?