iphone - 不同的警报 View 和 View Controller

标签 iphone objective-c uiviewcontroller uialertview code-reuse

在同一个 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/

相关文章:

ios - YouTube OAuth2授权在重新授权时失败

ios - 使用 set point.x iphone 获取 UIBezierPath point.y

iphone - 在父 ViewController 中获取当前 ViewController 名称

ios - 不相关的 UIViewController 的 viewDidLoad 被调用

iphone - 首先停用 UIModalTransitionStylePartialCurl 打开新的 UIVIewController

iphone - 解锁 iPhone 设备以继续吗?

iphone - 通过触摸内部激活uitableview

ios - 如何在Pod项目ViewController中导入AppDelegate.h文件?

ios - 加载不同的 xib

objective-c - 帮助理解多 Controller Cocoa 应用程序