开发者

Help me understand understand how to access UINaviagtionController using this implementation

开发者 https://www.devze.com 2023-03-23 13:34 出处:网络
So I\'m building an app and I am running through a few ViewControllers that don\'t need to know about each other, so I start off switching through views like so...

So I'm building an app and I am running through a few ViewControllers that don't need to know about each other, so I start off switching through views like so...

    // remove the previous view in order to load in the new view
    NSArray *sViews = [self.view subviews];
    [sViews makeObjectsPerformSelector:@selector(removeFromSuperview)];

    // create the new view, in this case the user wishes to 
    BaseViewController *baseVC = [[BaseViewController alloc] initWithNibName:@"BaseViewController" bundle:[NSBundle mainBundle]];
    self.baseViewController = baseVC;
    [baseVC release];

    // add the newly created view to the scr开发者_运维百科een
    [self.view insertSubview:baseViewController.view atIndex:0];

The above is the view controller that I want the navigation controller to reside in. So within the .m of this view controller I created a UINavigationController as a member variable and named it navController. I then tried implementing a UINavigationController using the code below.

    UIViewController *control = [[BusinessDisplayViewController alloc] initWithNibName:@"BusinessDisplayViewController" bundle: nil];
    navController = [[UINavigationController alloc] initWithRootViewController:control];
    [self presentModalViewController:navController animated:YES];

The problem I'm running into is two fold. First, when the BusinessDisplayViewController (below) is loaded there is a 20 pixel or so gap between my mapView and tableView that isn't there when I was loading it using insertSubview: not sure why that would be. Second, once I'm in BusinessDisplayViewController.m I'm not sure how to access the navigationController created in BaseViewController. Could someone explain why my view would be effected, how I could access the navigationController or if I'm even going about this the right way.


UINavigationController is designed for use in one of three possible contexts on iPhone:

  1. As the app's root view controller, with its view added as a subview of the app's window.
  2. As one of the viewControllers of a UITabBarController.
  3. Presented as a full screen view controller via presentModalViewController:animated:.

In your case, the UINavigationController has configured itself for presentation as a subview of the window. This is why you see the 20 pixel gap at the top. Since the window object underlaps the status bar, UINavigationController offsets the position of its navigation bar by 20 pixels (or more, if you're on a phone call).

The standard way to use UINavigationController as your root view controller is to construct it as a property of your app delegate in application:didFinishLaunchingWithOptions:, and add its view as a subview of the window. Then within any view controller you push onto your navigation stack, you can access the navigation controller object using self.navigationController.


Usually, you want your UINavigationController to be at the root level, Is there a specific reason for having your app setup this way? To answer your question though, you can access the variable by setting a property for it, then using the dot notation: baseVC.navController.

For the 20 pixel space problem, post your BaseViewController view related code. It is probably a bounds vs frame issue.

0

精彩评论

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