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