简体   繁体   English

不同的警报视图和viewcontroller

[英]different alert views and viewcontroller

In a same view controller, I have to show different alerts with different actions triggered by the alert buttons (this view controller is the delegate of the alerts). 在同一个视图控制器中,我必须显示由警报按钮触发的不同操作的不同警报(此视图控制器是警报的委托)。

Is there a way to reuse the alert code init/show/release, considering that in 有没有办法重用警报代码init / show / release,考虑到

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

I need the alerts to be distinguishable. 我需要将警报区分开来。

You can define a set of constants to represent each different type of alert view you're managing. 您可以定义一组常量来表示您正在管理的每种不同类型的警报视图。 For instance: 例如:

enum {
    MyFirstTypeOfWarning,
    MySecondTypeOfWarning
};
typedef NSInteger SPAlertViewIdentifier;

Then, whenever you find yourself needing to present a UIAlertView, call a method that wraps up the init/show show code and sets the tag of the UIAlertView: 然后,每当您发现自己需要呈现UIAlertView时,请调用包装init / show show代码并设置UIAlertView标记的方法:

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

Then, in alertView:clickedButtonAtIndex: you can check the tag of the alert view passed in and respond accordingly. 然后,在alertView:clickedButtonAtIndex中:您可以检查传入的警报视图的标记并进行相应的响应。

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

You can get the alert view here itself 您可以在此处获取警报视图

    - (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
//}
    }

Exactly what was stated by 7KV7. 正是7KV7所说的。 You need to tag the alert view, and check in your event handler what the sender's tag was. 您需要标记警报视图,并在事件处理程序中检查发件人的标记是什么。 This way, you can create different actions for different alert views in the same eventhandler (clickedButtonAtIndex). 这样,您可以在同一个事件处理程序中为不同的警报视图创建不同的操作(clickedButtonAtIndex)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM