如何创建导航控制器以将主视图控制器连接到 2 个自定义视图控制器
我需要一些有关如何创建将其根视图控制器加载到主视图控制器的导航控制器的帮助。然后,我想让导航控制器有两个选择器按钮(leftBarButtonItem 和 rightBarButtonItem),将用户带到 2 个自定义视图控制器(AboutViewController 和 SettingsViewController)。
我成功地做到了这一点,但相反,我在 MainWindow.xib 中创建了 2 个视图控制器,并将所有代码放在 AppDelegate.h 和 AppDelegate.m 中。这显然不是一个好的做法,尤其是当您有很多自定义视图控制器时。
谢谢。
I need some help on how to create a Navigation Controller that loads its Root View Controller to a Main View Controller. Then, I want to have that Navigation Controller two selector buttons (leftBarButtonItem and rightBarButtonItem) that will take the user to 2 custom View Controllers (AboutViewController and SettingsViewController).
I was able to do this successfully but instead, I created the 2 view controllers inside the MainWindow.xib with all codes inside AppDelegate.h and AppDelegate.m. This is obviously not a good practice especially when you have a lot of custom view controllers.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你是对的,在 AppDelegate 中创建视图控制器并不是一个好的做法。相反,您应该为每个视图创建一个单独的 ViewController。在 rootViewController 的实现文件中,创建方法来推送要在按下按钮时显示的单独视图控制器。像这样的事情会起作用:
然后,将这些方法包含在按钮的选择器字段中。像这样:
这应该能够推动你的观点。导航控制器将自动显示一个“返回”按钮。
在 AppDelegate 中,您将创建 navigationController 并告诉它哪个视图将成为 rootViewController,如下所示:
希望这会有所帮助!
You are right that it isn't good practice to create your view controllers in the AppDelegate. Instead, you should create a separate ViewController for each view. In the implementation file for your rootViewController, create methods to push the separate view controllers that you want to show when the buttons are pressed. Something like this would work:
Then, include those methods in the selector field for your buttons. Like this:
This should take care of pushing your view. The navigationController will automatically show a button to 'go back'.
In the AppDelegate, you would create the navigationController and tell it what view will be the rootViewController, like this:
Hope this helps!