开发者

How to avoid always creating UINavigationControllers?

开发者 https://www.devze.com 2023-04-01 03:39 出处:网络
In my app, I have a lot of modal views which need to be presented in a navigation controller, so I end up doing a lot of stuff like this:

In my app, I have a lot of modal views which need to be presented in a navigation controller, so I end up doing a lot of stuff like this:

MyModalController *modal = [[MyModalController alloc] init];
UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:modal];
[modal release];

[self presentModalViewController:navCon];
[navCon release];

Ideally, I'd like to simplify this, so that MyModalController takes care of creating the navigation controller. Any best practices for开发者_如何学JAVA this sort of thing? I'm thinking I could always just add a method like +navigationControllerWithModalController, but I'd like to hear how other people do it.

Also, I'd like to be able to attach a delegate to MyModalController, so I can send information back to the current view controller, so I know when to dismiss it.


So the views need each to be presented in a unique navigation controller? Then you might reverse your plan slightly and write something like:

@interface UINavigationController (NavigationControllerWithViewController)

+ navigationControllerWithRootViewControllerOfType:(Class)type;

@end

/* ... */


@implementation UINavigationController (NavigationControllerWithViewController)

+ navigationControllerWithRootViewControllerOfType:(Class)type
{
     UIViewController *modal = [[type alloc] init];
     UINavigationController *navCon = [[UINavigationController alloc] 
                                               initWithRootViewController:modal];
     [modal release];

     return [navCon autorelease];
}

@end

So you've declared a category on UINavigationController (that is, a way to add new methods to the class without subclassing it or having access to the original source) that does the common stuff of instantiating a view controller, creating a related navigation controller and returning it. You can then do:

UINavigationController *controller =
        [UINavigationController navigationControllerWithRootViewControllerOfType:
                    [MyModalController class]];

[self presentModalViewController:controller];

Or pass in whatever type of view controller you want in place of MyModalController. If you pass a class that isn't a UIViewController then you'll end up passing something that isn't a view controller to UINavigationController -initWithRootViewController: method, so be careful of that.

As for delegates, I guess you'd do the usual declaration of a protocol and a delegate, then add an extra parameter to your navigation controller category method to supply a delegate. You'll lose both helpful and unhelpful compile time warnings, but you'd probably want to do:

[(id)modal setDelegate:delegate];

That'll cause an exception if you attempt to instantiate a navigation controller with something that doesn't have a delegate property.

0

精彩评论

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