I have following structure in my iOS app:
- RootViewController (table view controller showing core data).
- 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 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...
You can just push the To-be-presented viewController into the current( presenting) viewcontroller's navigationController.
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.
Add navigation controller when present view controller
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.In your AppDelegate didFinishLaunchingWithOptions