简体   繁体   English

帮助如何从UIViewController加载UINavigationViewController

[英]Help how to load UINavigationViewController from UIViewController

Here goes stupid question again :( I am trying to create a sample app that loads UIViewController when the app loads and the view contains a button to load UINavigationViewController. 这又是一个愚蠢的问题:(我正在尝试创建一个示例应用程序,该应用程序在加载应用程序且视图包含用于加载UINavigationViewController的按钮时加载UIViewController。

Now I created a project with "Window-based Application" and added "RootViewController" in the project with .m, .h, and .xib. 现在,我使用“基于窗口的应用程序”创建了一个项目,并在项目中添加了带有.m,.h和.xib的“ RootViewController”。

Next I added a view and a button in the "RootViewController.xib" file and it runs ok. 接下来,我在“ RootViewController.xib”文件中添加了一个视图和一个按钮,它运行正常。 After that, I added "UIViewController subclass" file naming "NavViewController" with .h, .m and .xib files. 之后,我添加了“ UIViewController子类”文件,将“ NavViewController”命名为.h,.m和.xib文件。

Also I added - (IBAction)buttonPressed:(id)sender function in the "RootViewController" classes to load NavigationViewController. 我还在“ RootViewController”类中添加了-(IBAction)buttonPressed:(id)sender函数,以加载NavigationViewController。

Here is the code of the "buttonPressed:". 这是“ buttonPressed:”的代码。

- (IBAction)buttonPressed:(id)sender {
    NavViewController *navViewController = [[NavViewController alloc] initWithNibName:@"NavViewController" bundle:nil];
    self.navController = navViewController;
    [self.view insertSubview:navViewController.view atIndex:0];
    [navViewController release];
}

When I "build and go," it runs fine initially until I press the button. 当我“构建并运行”时,它一直运行良好,直到按下按钮为止。 When I press button, program terminates it. 当我按下按钮时,程序终止它。

What am I doing wrong? 我究竟做错了什么? Please help... 请帮忙...

Thank you. 谢谢。

What are you doing wrong? 你在做什么错 Designing your app in a non-standard way - you are not supposed to be able to do this - the NavigationController is in charge! 以非标准方式设计应用程序-您不应该能够执行此操作-NavigationController负责!

Why would you have a button that then adds a navigation controller? 为什么会有一个按钮然后添加导航控制器? - it goes against the user interface guidelines. -它违反了用户界面准则。 I found it hard to get to grips with the interface guidelines to begin with but you really must because it will make your app so much more usable. 我发现很难一开始就掌握界面准则,但您确实必须这么做,因为它会使您的应用更加实用。

If you need a navigation controller then add it to the view to begin with - or create a new view with the navigation controller. 如果您需要导航控制器,则将其添加到视图中-或使用导航控制器创建新视图。 Honestly try it out and you will feel the user interface feels much better. 诚实地尝试一下,您会觉得用户界面感觉好多了。

If you really want a button that adds a navigation controller to the window then do the following: 如果您确实想要一个将导航控制器添加到窗口的按钮,请执行以下操作:

  1. Keep a reference to the AppDelegate in your code 在您的代码中保留对AppDelegate的引用
  2. Use this reference and pass in your current view controller to a method called reloadMainViewWithNavBar:(UNViewController*) viewController 使用此引用并将当前的视图控制器传递给名为reloadMainViewWithNavBar:(UNViewController *)viewController的方法
  3. This new method should remove the old mainViewController and create a NavigationController using your viewController as the root view conroller 此新方法应删除旧的mainViewController并使用您的viewController作为根视图控制器创建一个NavigationController
  4. add the navigation controller view to window view 将导航控制器视图添加到窗口视图

The navController has no view set in the nib. navController在笔尖中未设置任何视图。 A UINavigationcontroller needs a root view. UINavigationcontroller需要一个根视图。 In the nib, connect the navigation controller to a another view controller. 在笔尖中,将导航控制器连接到另一个视图控制器。

In addition to the fix mentioned above, what you really want to do is create the first view controller contained in the navigation controller - but hide the navigation bar until the button press causes it to unhide. 除了上面提到的修复程序之外,您真正想做的是创建导航控制器中包含的第一个视图控制器-但隐藏导航栏,直到按下按钮使其取消隐藏。 You cannot easily add a navigation controller to a view you are in. 您无法轻松地将导航控制器添加到您所在的视图中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM