开发者

Temporary delegate instances in in objective-c

开发者 https://www.devze.com 2023-02-21 16:22 出处:网络
I\'m trying to write some generic http response handler functions that sometimes open UIAlertViews. These generic handlers are class methods don\'t have knowledge of their callers (at the moment).

I'm trying to write some generic http response handler functions that sometimes open UIAlertViews.

These generic handlers are class methods don't have knowledge of their callers (at the moment).

But I'm facing an obvious problem about how to alloc/dealloc the UiAlertView delegate object.

e.g.

MyAlertViewHandler* alertHandler = [[MyAlertViewHand开发者_高级运维ler alloc] init];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:msg message:nil delegate:alertHandler cancelButtonTitle:TEXT_NEVERMIND otherButtonTitles:TEXT_RESET_PASSWORD,nil];
[alert show];
[alert autorelease];

There's an obvious memory leak there cos I'm alloc'ing and not release it anywhere.

So, where do I "hang" MyAlertViewHandler such that I can release it to avoid the memory leak?

One idea is to tell the caller there's a NSObject that needs to be released and give responsibility back to the UIViewController - but is there another way?

I hope my question is clear enough.


In your alertView:didDismissWithButtonIndex: delegate method (in your MyAlertViewHandler) you could simply [self release]; or [self autorelease]; as the very last command.

0

精彩评论

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