不同的警报视图和viewcontroller

Sef*_*an2 2 iphone code-reuse objective-c uiviewcontroller uialertview

在同一个视图控制器中,我必须显示由警报按钮触发的不同操作的不同警报(此视图控制器是警报的委托).

有没有办法重用警报代码init/show/release,考虑到

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
Run Code Online (Sandbox Code Playgroud)

我需要将警报区分开来.

Ste*_*tto 7

您可以定义一组常量来表示您正在管理的每种不同类型的警报视图.例如:

enum {
    MyFirstTypeOfWarning,
    MySecondTypeOfWarning
};
typedef NSInteger SPAlertViewIdentifier;
Run Code Online (Sandbox Code Playgroud)

然后,每当您发现自己需要呈现UIAlertView时,请调用包装init/show show代码并设置UIAlertView标记的方法:

- (void)initializeAndPresentUIAlertViewForWarningType:(SPAlertViewIdentifier)tag {
    // Standard alloc/init stuff
    [alertView setTag:tag];
    [alertView show];
 }
Run Code Online (Sandbox Code Playgroud)

然后,在alertView:clickedButtonAtIndex中:您可以检查传入的警报视图的标记并进行相应的响应.

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if ([alertView tag] == MyFirstTypeOfWarning) {
        // Process button index for first type of alert.
    } ...
}
Run Code Online (Sandbox Code Playgroud)