如何避免总是创建 UINavigationControllers?
在我的应用程序中,我有很多需要在导航控制器中呈现的模态视图,因此我最终做了很多这样的事情:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那么每个视图都需要在唯一的导航控制器中呈现吗?然后,您可以稍微扭转您的计划并编写如下内容:
因此,您已经在 UINavigationController 上声明了一个类别(即,一种向类添加新方法而无需对其进行子类化或无法访问原始方法的方法)源),它执行实例化视图控制器、创建相关导航控制器并返回它的常见操作。然后您可以执行以下操作:
或者传入您想要的任何类型的视图控制器来代替
MyModalController
。如果您传递的类不是UIViewController
,那么您最终会将不是视图控制器的内容传递给UINavigationController -initWithRootViewController:
方法,所以要小心的。至于委托,我想您会执行协议和委托的常规声明,然后向导航控制器类别方法添加一个额外的参数以提供委托。您将失去有用和无用的编译时警告,但您可能想要这样做:
如果您尝试使用不具有委托属性的内容实例化导航控制器,则会导致异常。
So the views need each to be presented in a unique navigation controller? Then you might reverse your plan slightly and write something like:
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:Or pass in whatever type of view controller you want in place of
MyModalController
. If you pass a class that isn't aUIViewController
then you'll end up passing something that isn't a view controller toUINavigationController -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:
That'll cause an exception if you attempt to instantiate a navigation controller with something that doesn't have a delegate property.