No navigation bar in view

2019-08-17 05:42发布

I have following structure in my iOS app:

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

RootViewController

  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.

DoneViewController

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...

5条回答
Viruses.
2楼-- · 2019-08-17 06:04

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];
}
查看更多
看我几分像从前
3楼-- · 2019-08-17 06:08

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楼-- · 2019-08-17 06:18

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];
}
查看更多
闹够了就滚
5楼-- · 2019-08-17 06:18

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.

查看更多
干净又极端
6楼-- · 2019-08-17 06:21

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];
查看更多
登录 后发表回答