没有导航栏 [英] No navigation bar in view

查看:24
本文介绍了没有导航栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 iOS 应用中有以下结构:

I have following structure in my iOS app:

  1. RootViewController(显示核心数据的表视图控制器).

  1. RootViewController 上有一个包含 4 个按钮的视图.
  2. 按钮 1:菜单...
  3. 按下按钮 1 后,NSManagedObjectContext 被传递给 MenuViewController.
  4. 在 MenuViewController 上有几个按钮,其中一个用于打开 DoneViewController,它是 RootViewController 的副本(仅将 NSPredicates 更改为显示不同的核心数据对象.
  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 正确显示了预期的行,但它应该有一个导航栏,但未显示.

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

这是用于打开DoneViewController的代码:

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

我应该怎么做才能打开视图并拥有像 RootViewController 那样的导航栏?

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

@EricLee 提出的最后一个 doneToDoaction 方法没有抛出异常,但是按钮动作没有执行并且应用程序冻结...

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

推荐答案

当存在视图控制器时添加导航控制器

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

这篇关于没有导航栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆