在同一个 View Controller 中,我必须通过警报按钮触发的不同操作来显示不同的警报(此 View Controller 是警报的委托(delegate))。
有没有办法重用警报代码 init/show/release,考虑到
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
我需要能够区分警报。
最佳答案
您可以定义一组常量来表示您正在管理的每种不同类型的警报 View 。例如:
enum {
MyFirstTypeOfWarning,
MySecondTypeOfWarning
};
typedef NSInteger SPAlertViewIdentifier;
然后,每当您发现自己需要呈现 UIAlertView 时,请调用包装 init/show 显示代码并设置 UIAlertView 标签的方法:
- (void)initializeAndPresentUIAlertViewForWarningType:(SPAlertViewIdentifier)tag {
// Standard alloc/init stuff
[alertView setTag:tag];
[alertView show];
}
然后,在alertView:clickedButtonAtIndex:中可以检查传入的alert view的标签并做出相应的响应。
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if ([alertView tag] == MyFirstTypeOfWarning) {
// Process button index for first type of alert.
} ...
}
关于iphone - 不同的警报 View 和 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5511590/