开发者

Presenting a Navigation Controller

开发者 https://www.devze.com 2023-03-13 18:29 出处:网络
I am trying to present a NavigationController from a regular view controller. When I present the nav controller, all I see is the default navigation bar (no matter what I set it to in IB), which makes

I am trying to present a NavigationController from a regular view controller. When I present the nav controller, all I see is the default navigation bar (no matter what I set it to in IB), which makes me think the navigation controller isn't liked right in IB. This is my code to open the navigation controller:

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

Here is my NavigationController header:

@inter开发者_运维百科face NavigationController : UINavigationController <UINavigationControllerDelegate> {
    UINavigationController *navigationController;
}

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

@end

And navigationController is synthesized in the implementation.

In IB, I have a navigation controller that is connected to navigationController and the file's owner delegate. What am I doing wrong? Thanks

EDIT: This is what it looks like in IB:

Presenting a Navigation Controller

And this is what is looks like in the simulator:

Presenting a Navigation Controller


What you currently have is a subclass of a UINavigationController. That would be what you want to do if you wanted to add custom functionality to UINavigationController (like doing a different animation between views). Since it sounds like you want to make a navigation based application, you actually just want to use the plain UINavigationController class. Here is what you what you have to do to get a UINavigationController set up with the content that you want. (I am doing this in Code because I hate having to set up UINavigationController's in IB).

In your applicationDidFinishLaunching, you want to add this bit of code.

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

This will create a UINavigationController and set up your table view as the content of that. When you want to do the nice animation to a new view, you do this

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

This will automatically do the animation to slide to the next view controller, along with making the nice back button to bring you back to your ContentViewController

EDIT: To make the navigation controller show up as a modal view controller, this is what you do. Instead of using the above code in the application delegate, you do this.

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

Then, when you are done with the navigation controller, and want it to go away, you do this:

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


Declare a navigation controller in your viewDidLoad? so:

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


navigationController.view is blank by default, you will have to put something there.

0

精彩评论

暂无评论...
验证码 换一张
取 消