0

I have a methods that clears all my UITextFields in the UIViewController. I have a lot of methods that trigger that function before taking place. I want to ask the user using an UIAlertView if it's ok to clear fields before the action is taking place. I'm aware of alertView:clickedButtonAtIndex: but don't really want to use it because my switch will look like this:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(alertView.tag == 2000)
    {
        if (buttonIndex == 0)
        {
          // do stuff
        }
        else if (buttonIndex == 1)
        {
            // do stuff
        }
    }
    if(alertView.tag == 3000)
    {
        if (buttonIndex == 0)
        {
          // do stuff
        }
        else if (buttonIndex == 1)
        {
            // do stuff
        }
    }
etc..

I'm searching for an elegant way to trigger the same UIAlertView before every function that needs to clear the screen before it's triggered.

Thanks

4

1 回答 1

-2
@interface 

//Keep a global references or repeated use...
@property(nonatomic, retain) UIAlertView *myAlertView;

@end



@implementation

-(void)alertViewCallingMethod
{
    //Generate the alertView on the same handle for repeated use.
     myAlertView = [[UIAlertView alloc] initWith..... ];
     myAlertView.delegate = self;
     [myAlertView show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(alertView == myAlertView)
    {
        //do you stuff...
    }
}

@end

这是关于我的答案的未取消投票的补充:

提问者最后说

I'm searching for an elegant way to trigger the same UIAlertView before every function that needs to clear the screen before it's triggered.

根据上述要求,我对这个问题的回答是最合适的。我给了你答案,可以使用相同的句柄重复使用 alertView 对象,这又删除了标记单独 UIAlertView 的所有不必要的检查,并为委托中的每个对象编写检查代码。

于 2013-08-12T09:40:06.070 回答