Iphone 介绍一种导航控制器

Iphone 介绍一种导航控制器,iphone,objective-c,uinavigationcontroller,Iphone,Objective C,Uinavigationcontroller,我正在尝试从常规视图控制器演示NavigationController。当我展示导航控制器时,我看到的只是默认的导航栏(无论我在IB中设置了什么),这让我觉得导航控制器在IB中不受欢迎。这是我打开导航控制器的代码: NavigationController *navController = [[NavigationController alloc] initWithNibName:@"NavigationController" bundle:[NSBundle mainBundle]]; [se

我正在尝试从常规视图控制器演示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
@界面导航控制器:UINavigationController{
UINavigationController*导航控制器;
}
@属性(非原子,保留)IBuinavigationController*导航控制器;
@结束
在实现中综合了导航控制器

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

编辑:这是IB中的外观: 这就是模拟机中的情况:

导航控制器.view默认为空,您必须在那里放置一些东西。

导航控制器.view默认为空,您必须在那里放置一些东西。

在您的viewDidLoad中声明导航控制器? 因此:


在viewDidLoad中声明导航控制器? 因此:


您当前拥有的是UINavigationController的子类。如果您想向UINavigationController添加自定义功能(如在视图之间执行不同的动画),那么这就是您想要做的。因为听起来您想创建一个基于导航的应用程序,所以实际上您只想使用普通的UINavigationController类。下面是您必须做的事情,以便使用所需的内容设置UINavigationController。(我在代码中这样做是因为我讨厌在IB中设置UINavigationController)

在ApplicationIDFinishLaunching中,您希望添加这段代码

// (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];

您当前拥有的是UINavigationController的子类。如果您想向UINavigationController添加自定义功能(如在视图之间执行不同的动画),那么这就是您想要做的。因为听起来您想创建一个基于导航的应用程序,所以实际上您只想使用普通的UINavigationController类。下面是您必须做的事情,以便使用所需的内容设置UINavigationController。(我在代码中这样做是因为我讨厌在IB中设置UINavigationController)

在ApplicationIDFinishLaunching中,您希望添加这段代码

// (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];

我不认为你目前的设置是你想要的。你介意告诉我们一些关于你正在尝试用这个导航控制器做什么的事情吗?您是希望向现有的UINavigationController添加功能,还是希望创建一个基于导航的应用程序(类似于邮件或音乐),为您提供后退按钮和漂亮的动画。@Scott Rice我已经有了一个应用程序,只是我想添加一个导航控制器。在我让它工作之后,我将向它添加一个表视图。我不认为你当前的设置完全是你想要的。你介意告诉我们一些关于你正在尝试用这个导航控制器做什么的事情吗?您是希望向现有的UINavigationController添加功能,还是希望创建一个基于导航的应用程序(类似于邮件或音乐),为您提供后退按钮和漂亮的动画。@Scott Rice我已经有了一个应用程序,只是我想添加一个导航控制器。当我在模拟器中打开导航控制器时,它会在顶部显示一个蓝色(默认)导航栏。这是否意味着应用程序没有显示与导航控制器对应的正确视图?不,这是预期的,请使用[navigationController.view addSubview:someView];要添加视图,在导航项目中,它会自动启动,并将其设置为添加rootViewController.view。当我在显示导航控制器之前添加[navigationController.view addSubview:tableView.view]时,当我在模拟器中打开导航控制器时,它在顶部显示一个蓝色(默认)导航栏。这是否意味着应用程序没有显示与导航控制器对应的正确视图?不,这是预期的,请使用[navigationController.view addSubview:someView];要添加视图,在导航项目中,它会自动启动,并将其设置为添加rootViewController.view。当我在显示导航控制器之前添加[navigationController.view addSubview:tableView.view]时,它会在屏幕的一半显示表视图w