开发者

How do I use a modalViewController Identically in Two Controllers?

I'm using the Three20 TTMessageController in my app. I've figured out how to use it, adding on a bunch of other stuff (including TTMessageControllerDelegate methods and ABPeoplePickerNavigationControllerDelegate methods). It works great for me, after a bit of a struggle to figure it out.

The trouble I'm having now is a design issue: I want to use it identically in two different places, including with the same delegate methods. My current approach is that I've put all the code into a single class inheriting from NSObject, called ComposerProxy, and I'm just having the two controllers that use it use the proxy, like so:

ComposerProxy *proxy = [[ComposerProxy alloc] initWithController:this];
[proxy go];

The go method constructs the TTMessageController, configures it, adds it to a UINavigationController, and presents it:

[self.controller presentModalViewController: navController animated: YES];

This works great, as I have all my code nicely encapsulated in ComposerProxy and I need only the above two lines anywhere I want to use it.

The downside, though, is that I can't dealloc the proxy variable without getting crashes. I can't autorelease it, either: same problem.

So开发者_高级运维 I'm wondering if my proxy approach is a poor one. How does one normally encapsulate a bunch of behaviors like this without requiring a lot of duplicate code in the classes that use it? Do I need to add a delegate class to my ComposerProxy and make the controller responsible for dismissing the modal view controller in a hypothetical composerDidFinish method or some such?

Many TIA!


From what I see above, the crashes don't necessarily indicate a poor design - chances are it's crashing on a memory management issue. Maybe controller is over-released, hard to say - what kind of crash are you getting?

While the current design seems fine, an alternative would be to create a category on UIViewController. The category would add (to UIViewController subclasses that import the category) all the code necessary for presenting a modal TTMessageController without requiring you to duplicate or use inheritance.

@interface UIViewController ( Composer )
// categories can't add instance vars, so return the new controller if you need to store it...
- (TTMessageController *)presentMessageController;
@end

@implementation UIViewController ( Composer )
- (TTMessageController *)presentMessageController {
    // contents of ComposerProxy#go except referring to 'self' instead of 'self.controller'
}
@end
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜