问题描述
我的 iOS 应用中有以下结构:
I have following structure in my iOS app:
- RootViewController(显示核心数据的表视图控制器).
- RootViewController 上有一个包含 4 个按钮的视图.
- 按钮 1:菜单...
- 按下按钮 1 后,
NSManagedObjectContext
被传递给 MenuViewController. - 在 MenuViewController 上有几个按钮,其中一个用于打开 DoneViewController,它是 RootViewController 的副本(仅将
NSPredicates
更改为显示不同的核心数据对象.
- On RootViewController is a view including 4 buttons.
- Button 1: Menu...
- After pressing button 1,
NSManagedObjectContext
is passed to MenuViewController. - 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];
}
这篇关于没有导航栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!