【问题标题】:Presenting a Navigation Controller展示一个导航控制器
【发布时间】:2011-06-16 08:34:12
【问题描述】:

我正在尝试从常规视图控制器中呈现 NavigationController。当我展示导航控制器时,我看到的只是默认导航栏(无论我在 IB 中将其设置为什么),这让我认为导航控制器在 IB 中不受欢迎。这是我打开导航控制器的代码:

NavigationController *navController = [[NavigationController alloc] initWithNibName:@"NavigationController" bundle:[NSBundle mainBundle]];
[self presentModalViewController:navController animated:YES];

这是我的 NavigationController 标头:

@interface NavigationController : UINavigationController <UINavigationControllerDelegate> {
    UINavigationController *navigationController;
}

@property (nonatomic,retain) IBOutlet UINavigationController *navigationController;

@end

并且navigationController在实现中被合成。

在 IB 中,我有一个连接到 navigationController 和文件所有者委托的导航控制器。我究竟做错了什么?谢谢

编辑:这就是它在 IB 中的样子: 这就是模拟器中的样子:

【问题讨论】:

  • 我认为您当前的设置并不是您想要的。你介意告诉我们一些关于你试图用这个导航控制器做什么的事情吗?您是否希望为现有的 UINavigationController 添加功能,或者您是否希望创建一个基于导航的应用程序(类似于邮件或音乐),为您提供返回按钮和漂亮的动画。
  • @Scott Rice 我已经有一个应用程序,只是我想添加一个导航控制器。在我让它工作之后,我要给它添加一个表格视图。

标签: iphone objective-c uinavigationcontroller


【解决方案1】:

您目前拥有的是 UINavigationController 的子类。如果您想向 UINavigationController 添加自定义功能(例如在视图之间制作不同的动画),那将是您想要做的。由于听起来您想创建一个基于导航的应用程序,因此您实际上只想使用普通的 UINavigationController 类。以下是您必须做的事情,才能让 UINavigationController 设置您想要的内容。 (我在 Code 中这样做是因为我讨厌在 IB 中设置 UINavigationController)。

在你的 applicationDidFinishLaunching 中,你想添加这段代码。

// (SomethingApplicationDelegate.m)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  // Assume that 'window' is your main window, and 'viewController' is the property
  // that is automatically created when you do a new view controller application.
  //
  // Also, assume that 'ContentViewController' is the name of the class that display
  // the table view. This will most likely be a subclass of UITableViewController
  ContentViewController *content = [[ContentViewController alloc] init];
  UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:content];
  [viewController.view addSubview:nvc.view];
  // This is the boilerplate code that is generated when you make a new project
  self.window.rootViewController = self.viewController;
  [self.window makeKeyAndVisible];
  return YES;
}

这将创建一个 UINavigationController 并将您的表格视图设置为其内容。当您想为新视图制作漂亮的动画时,您可以这样做

// This will be inside your ContentViewController
// Assume that 'NewViewController' is the class of the view controller you want
// to display
NewViewController *viewController = [[NewViewController alloc] init];
[self.navigationController pushViewController:viewController];
[viewController release]

这将自动执行动画以滑动到下一个视图控制器,同时制作漂亮的后退按钮将您带回 ContentViewController

编辑:要使导航控制器显示为模态视图控制器,这就是您要做的。您可以这样做,而不是在应用程序委托中使用上述代码。

-(IBAction)buttonPushed:(id)sender {
  // Assume this method is in a UIViewController subclass.
  //
  // The next two lines are copied from above in the Application Delegate, the same assumptions
  // apply about ContentViewController
  ContentViewController *content = [[ContentViewController alloc] init];
  UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:content];
  // This is the magic code
  [self presentModalViewController:nvc];
}

然后,当您完成导航控制器并希望它消失时,您可以这样做:

// Assume we are in some method in ContentViewController, or a similar view controller that is showing content
[self.navigationController dismissModalViewController];

【讨论】:

  • 谢谢,但我不需要在启动应用程序时显示导航控制器。我需要在用户按下按钮后以模态方式呈现它。为了做到这一点,我将如何修改此代码?
  • 它工作得很好,我不知道做起来这么简单。非常感谢。
【解决方案2】:

在您的 viewDidLoad 中声明一个导航控制器? 所以:

UINavigationController *navController = [[UINavigationController alloc]
                                           initWithRootViewController:view];

【讨论】:

    【解决方案3】:

    navigationController.view 默认是空白的,你必须在里面放一些东西。

    【讨论】:

    • 当我在模拟器中打开导航控制器时,它会在顶部显示一个蓝色(默认)导航栏。这是否意味着应用程序没有显示与导航控制器对应的正确视图?
    • 不是预期的,使用 [navigationController.view addSubview:someView];要添加视图,在导航项目中它会自动启动并设置为添加 rootViewController.view。
    • 当我在显示导航控制器之前添加 [navigationController.view addSubview:tableView.view] 时,它会在没有导航栏的一半屏幕上显示表格视图。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-09
    • 2020-10-02
    • 2013-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多