如何避免总是创建 UINavigationControllers?

发布于 2024-12-02 10:07:43 字数 546 浏览 1 评论 0原文

在我的应用程序中,我有很多需要在导航控制器中呈现的模态视图,因此我最终做了很多这样的事情:

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

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

理想情况下,我想简化它,以便 MyModalController 负责创建导航控制器。对于此类事情有什么最佳实践吗?我想我总是可以添加一个像 +navigationControllerWithModalController 这样的方法,但我想听听其他人是如何做到的。

另外,我希望能够将委托附加到 MyModalController,这样我就可以将信息发送回当前视图控制器,这样我就知道何时关闭它。

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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小女人ら 2024-12-09 10:07:43

那么每个视图都需要在唯一的导航控制器中呈现吗?然后,您可以稍微扭转您的计划并编写如下内容:

@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

因此,您已经在 UINavigationController 上声明了一个类别(即,一种向类添加新方法而无需对其进行子类化或无法访问原始方法的方法)源),它执行实例化视图控制器、创建相关导航控制器并返回它的常见操作。然后您可以执行以下操作:

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

[self presentModalViewController:controller];

或者传入您想要的任何类型的视图控制器来代替 MyModalController。如果您传递的类不是 UIViewController ,那么您最终会将不是视图控制器的内容传递给 UINavigationController -initWithRootViewController: 方法,所以要小心的。

至于委托,我想您会执行协议和委托的常规声明,然后向导航控制器类别方法添加一个额外的参数以提供委托。您将失去有用和无用的编译时警告,但您可能想要这样做:

[(id)modal setDelegate:delegate];

如果您尝试使用不具有委托属性的内容实例化导航控制器,则会导致异常。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文