简体   繁体   English

将导航控制器添加到选项卡栏应用程序

[英]Adding a navigation controller to a tab bar application

I have two questions about this one. 我对此有两个问题。 First, I have the navigation controller successfully put in the storyboard and is linked with the tabs and is working how I would want it to. 首先,我已将导航控制器成功放入情节提要中,并与选项卡链接,并且正在按我的意愿进行工作。 Except for one thing. 除了一件事。 When I try to add a code such as this 当我尝试添加这样的代码时
[self.navigationController popToViewController:vc animated:YES] I get an error Property 'navigationController' not found on object of type 'AppDelegate *' [self.navigationController popToViewController:vc animated:YES]我收到错误消息,在类型为'AppDelegate *'的对象上找不到属性'navigationController'

Is this because I put it in the wrong place? 这是因为我把它放错了地方吗? Or becasue its a tabbar application and something aint right. 或因为它是一个Tabbar应用程序和一些正确的东西。

It sounds like you're trying to make a call to your navigation controller from your AppDelegate. 听起来您正在尝试从AppDelegate调用导航控制器。 Unless you've specifically setup your AppDelegate to work with your navigation controller (it'd need to be a subclass of UIViewController ), you'll get an error because there is no Navigation Controller on your AppDelegate class (by default). 除非您专门设置了AppDelegate以使其与导航控制器一起使用(它必须是UIViewController的子类),否则会出现错误,因为AppDelegate类上没有导航控制器(默认情况下)。 Therefore, when you make that call - the navigation controller can't be found. 因此,当您拨打该电话时-找不到导航控制器。 Notice how your AppDelegate is a subclass of UIResponder , not UIViewController : 注意您的AppDelegate是UIResponder的子类,而不是UIViewController的子类:

@interface AppDelegate : UIResponder <UIApplicationDelegate>

Instead, create and / or connect your navigation controller to a UIViewController subclass - then you can make calls like this from your subclass: 而是创建和/或将导航控制器连接到UIViewController子类-然后可以从子类中进行如下调用:

[self.navigationController popToViewController:vc animated:YES];

To create and setup a Navigation Controller, follow these steps (may vary if you aren't using storyboards). 要创建和设置导航控制器,请执行以下步骤(如果您不使用故事板,则可能会有所不同)。

  1. Create a new UINavigationController Obj-C subclass. 创建一个新的UINavigationController Obj-C子类。 In the Xcode menu bar, select File > New , or press CMD + N . 在Xcode菜单栏中,选择File > New ,或按CMD + N。 Name your class and set its superclass as UINavigationController : 为您的类命名,并将其超类设置为UINavigationController 添加一个UINavigationController子类
    Note that you don't absolutely need an entirely new class! 请注意,您不一定不需要全新的课程! You can use an existing class that is a subclass of UIViewController - as long as the navigationController property is available. 您可以使用作为UIViewController子类的现有类-只要navigationController属性可用。
  2. Add your navigation controller from the objects library in Xcode and set it up the way you need it. 从Xcode的对象库中添加导航控制器,并根据需要进行设置。
  3. Select the NavigationController in your Storyboard, then open the Utilities Panel , and select the Identity Inspector Tab . 在情节提要中选择NavigationController,然后打开“ 实用工具”面板 ,然后选择“ 身份检查器”选项卡 Set the Custom Class name to the name of your UIViewController or UINavigationController subclass: Custom Class名称设置为您的UIViewControllerUINavigationController子类的名称: 导航控制器自定义类Xcode 4.6
  4. From your class you'll be able to use the navigationController property, among hundreds of others relating to the View Controller. 从您的类中,您将能够使用navigationController属性,以及与视图控制器相关的数百个其他属性。 Remember that the AppDelegate is really a place for setting up your app and handling app events (ex. app closing, app backgrounding, app opening). 请记住,AppDelegate实际上是设置应用程序和处理应用程序事件(例如,应用程序关闭,应用程序背景,应用程序打开)的地方。

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

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