开发者

Releasing a UIAlertViewDelagate

I have a view controller that need to handle a number of UIAlertViews, so ideally I don't want to set the delegate to self, and handle everything with tags on the alert view - unless that is the preferred approach.

If I create a class implementing UIAlertViewDelegate, where/how can I safely release it?

-(void)myMethod {
    MyUIAlertViewDelegate *myDelegate = [[MyUIAlertViewDelegate alloc] init];
    UIAlertView *m开发者_StackOverflowyAlertView = [[UIAlertView alloc] initWithTitle ... delegate:myDelegate ...
    [myAlertView show];
    [myAlertView release];

}

What recommendations are there for creating this type of custom delegate?


I’m unsure what you stand to gain out of this. Are you creating a new instance of the class for each alert view? That seems like unnecessary overhead to me. Using tags doesn’t have to be messy, though; just use the delegate methods to call into other methods that do what you want:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch ([alertView tag]) {
        case kWidgetATag:
          [self doSomethingForWidgetA];
        case kWidgetBTag:
          [self doSomethingForWidgetB];
    }
}


I think the best way to release your view controller would be in method that is called the last:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    [self release];
}


It seems strange to have a class that exists solely to handle the UIAlertView's methods.

Typically some view controller will have direct interest in the result of the UIAlertView's result. That view controller will implement the UIAlertViewDelegate protocol, create and present the UIAlertView (setting delegate to self), and subsequently receive its delegate messages.

The ViewController will then update View or Model concerns as needed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜