No navigation bar in view

2019-08-17 05:30发布

问题:

I have following structure in my iOS app:

  1. RootViewController (table view controller showing core data).

  1. On RootViewController is a view including 4 buttons.
  2. Button 1: Menu...
  3. After pressing button 1, NSManagedObjectContext is passed to MenuViewController.
  4. On MenuViewController there are several buttons, one of them is used to open DoneViewController, which is a duplicate from RootViewController (only NSPredicates changed to show different core data objects.

DoneViewController is showing correctly the expected rows, but it was supposed to have a navigation bar, which is not shown.

This is the code used to open DoneViewController:

- (IBAction)doneToDoaction:(id)sender {
    DoneViewController *viewController = [[DoneViewController alloc] init];
    viewController.managedObjectContext = [self mainContext];
    [self presentViewController:viewController animated:YES completion:nil];
}

What should I do to open the view and have a navigation bar like RootViewController does?


I will now put all the navigation controller instances that are now in my app:

//AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  // Fetch the data to see if we ought to pre-populate
  NSError *error = nil;
  if (![[self fetchedResultsController] performFetch:&error]) {
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
  }

  [self loadFavoriteThingsData];


  RootViewController *rootViewController = (RootViewController *)

    [navigationController topViewController];
  [rootViewController setManagedObjectContext:[self managedObjectContext]];


  [window addSubview:[navigationController view]];
  [window makeKeyAndVisible];
  return YES;
}

//RootViewController
- (IBAction)MenuToDoAction:(id)sender {

    MenuViewController *viewController = [[MenuViewController alloc] init];

    viewController.mainContext = [self managedObjectContext];
    [self presentViewController:viewController animated:YES completion:nil];

}

//MenuViewController
- (IBAction)doneToDoaction:(id)sender {
    DoneViewController *viewController = [[DoneViewController alloc] init];
    viewController.managedObjectContext = [self mainContext];
    [self.navigationController pushViewController:viewController animated:YES];
}

The last doneToDoaction method, as proposed by @EricLee, doesn't throw an exception, but the button action is not executed and the app freezes...

回答1:

Add navigation controller when present view controller

- (IBAction)doneToDoaction:(id)sender {
    DoneViewController *viewController = [[DoneViewController alloc] init];
    viewController.managedObjectContext = [self mainContext];
UINavigationController * nVC=[[UINavigationController alloc] initWithRootViewController:viewController];
    [self presentViewController:nVC animated:YES completion:nil];
}


回答2:

Instead of presenting DoneViewController, you want to push it onto the current navigation controller.

It is the navigation controller (self.navigationController) that has the view containing the navigation bar. Pushing a view controller onto it will cause that view controller's view to appear, along with the navigation bar, inside the navigation controller's view.



回答3:

If you use storyboard or xib and do not want to push DoneViewController to UINavigationViewController, I suggest you to make a gap for status bar manually in storyboard or xib.

In iOS7, the status bar is transparent. therefore, the top of your VC is extended under it.
If you push it to UINavigationViewController and navigation bar is opaque, there is nothing to do. But, if you don't do that, set a gap manually.

EDIT
Sorry, I found another way better than what I suggest you before. You don't need transparent status bar, set status bar style option to Black Opaque in General tab in project configuration.



回答4:

You can just push the To-be-presented viewController into the current( presenting) viewcontroller's navigationController.

- (IBAction)doneToDoaction:(id)sender {
    DoneViewController *viewController = [[DoneViewController alloc] init];
    viewController.managedObjectContext = [self mainContext];
    [self.navigationController pushViewController:viewController animated:YES];
}


回答5:

In your AppDelegate didFinishLaunchingWithOptions

self.window = [[UIWindo walloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.controller = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:self.controller];
self.window.rootViewController = nav;
[self.window addSubview:nav.view];
[self.window makeKeyAndVisible];