Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 不同的警报视图和viewcontroller_Iphone_Objective C_Uiviewcontroller_Uialertview_Code Reuse - Fatal编程技术网

Iphone 不同的警报视图和viewcontroller

Iphone 不同的警报视图和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

在同一个视图控制器中,我必须通过警报按钮触发的不同操作显示不同的警报(此视图控制器是警报的代理)

有没有办法重用警报代码init/show/release

- (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*),然后检查方法委托中的标记是否相等?