Universal Master-Detail Application with Core Data and tab controller for iphone storyboard gets unrecognized selector sent to instance error - ios

I started a Master-Detail Application in X-Code. I chose the options to be universal, core data, and git repo. When the application comes up, I went into the iphone story board, added a tab view controller, moved the nav/table/detail views it starts with to be in the tab controller as the third tab (in reality I want it to be the fourth). I then chose the tab controller to be the initial view the program should start with when in iphone mode. It builds successfully but does not allow the program to finish loading. The error that comes out is recorded below:
2013-05-11 21:35:00.302 FearlessAndThorough[6318:907] -[UITabBarController topViewController]: unrecognized selector sent to instance 0x1c592020
2013-05-11 21:35:00.306 FearlessAndThorough[6318:907] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITabBarController topViewController]: unrecognized selector sent to instance 0x1c592020'
*** First throw call stack:
(0x337f33e7 0x3b4ee963 0x337f6f31 0x337f564d 0x3374d208 0xc9e43 0x35662aa1 0x35662625 0x3565a833 0x35602d1f 0x356027ad 0x356021ef 0x3731a5f7 0x3731a227 0x337c83e7 0x337c838b 0x337c720f 0x3373a23d 0x3373a0c9 0x3565946d 0x356562b9 0xc9ab5 0x3b91bb20)
libc++abi.dylib: terminate called throwing an exception
(lldb)
I am hoping that someone has done this before and can give me a little insight as to the proper procedure or steps to take when setting up a tab view controller type app that will then convert into a master detail application for ipad.
Here is the current app delegate's didFinishLaunchingWithOptions:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
splitViewController.delegate = (id)navigationController.topViewController;
UINavigationController *masterNavigationController = splitViewController.viewControllers[0];
MasterViewController *controller = (MasterViewController *)masterNavigationController.topViewController;
controller.managedObjectContext = self.managedObjectContext;
} else {
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
MasterViewController *controller = (MasterViewController *)navigationController.topViewController;
controller.managedObjectContext = self.managedObjectContext;
}
return YES;
}

Your problem is that you added a tab bar controller to the front of your iPhone storyboard, but in your "else" clause you say the root view controller of the window is a navigation controller -- it's not, the tab bar controller that you added is. If you put a log in as the second line in that else clause, you will see that navigationController is actually a tab bar controller, not a navigation controller. That else clause needs to look like this:
UITabBarController *tbc = (UITabBarController *)self.window.rootViewController;
UINavigationController *navigationController = tbc.viewControllers[3];
MasterViewController *controller = (MasterViewController *)navigationController.topViewController;
controller.managedObjectContext = self.managedObjectContext;
I'm not sure about the "3" in tbc.viewControllers. I can't tell from your description which tab that navigation controller is in, so you might need to change that.

I think I got your problem.
Did you use a UITabbar object in the Xcode navigator :
Because to manage views with a TabbarController, the best way is to embed your view in a Navigation Controller, like below :
Then you got your first view embedded in a navigation controller :
You can now add a new viewController in the field and add it to the TabbarController by control-dragging from the TabbarController and select the Relationship item :
Then you got 2 views in a navigationController :
And nothing to do in the appDelegate.
Then do the last step for any other view you want embedded in the navigationController.
Hope this helps.

// Change your else part in appdelegate to this it may works.
else {
UITabBarController *tabBarController = (UITabBarController *) self.window.rootviewController;
// For third view in tabbar controller
UINavigationController *navigationController = [tabBarController viewControllers] objectAtIndex: 2];
navigationController = (UINavigationController *)self.window.rootViewController;
MasterViewController *controller = (MasterViewController *)navigationController.topViewController;
controller.managedObjectContext = self.managedObjectContext;
}

Related

Make SWRevealViewController as rootViewController after Sign In when it has tabbarController as sw_front objC

This is my Storyboards :
Say I have a Sign In viewController above these and from where I want to make SWRevealViewController as my rootViewController, so that It can work perfectly. With the below code, From my leftMenuViewController I can select my tabBar with desired ViewController perfectly.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITabBarController *tabBarController = (UITabBarController *)self.revealViewController.frontViewController;
UINavigationController *navController = tabBarController.viewControllers[indexPath.row];
[navController popToRootViewControllerAnimated:NO];
tabBarController.selectedIndex = indexPath.row;
self.revealViewController.rearViewRevealOverdraw = 0.0f;
[self.revealViewController pushFrontViewController:tabBarController animated:YES];
}
But it is not working in App delegate or in SignInViewController.
- (void)checkIfUserSignedIn
{
if ([ManagerClass getBOOLTypeUserDefaultForKey:#"isSignedIn"] == YES) {
UITabBarController *tabBarController = (UITabBarController *)self.revealViewController.frontViewController;
UINavigationController *navController = tabBarController.viewControllers[0];
[navController popToRootViewControllerAnimated:NO];
tabBarController.selectedIndex = 0;
self.revealViewController.rearViewRevealOverdraw = 0.0f;
[self.revealViewController pushFrontViewController:tabBarController animated:YES];
self.window.rootViewController = tabBarController;
} else {
}
}
It is giving this in log:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Application windows are expected to have a root view controller at the end of application launch'
Any help will be highly appreciated. Thanks a lot in advance.
Have a good day.
All I need to do is this:
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:mainStoryboard bundle:[NSBundle mainBundle]];
SWRevealViewController *controller = (SWRevealViewController *)[storyBoard instantiateViewControllerWithIdentifier:#"RevealViewControllerID"];
[self.window setRootViewController:controller];
Just set SWRevealViewController as your rootViewController of the project. :)
your answer above is right but the crash you mentioned in you question
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Application windows are expected to have a root view controller at the end of application launch'
was caused from you storyboard because the SWRevealVC in the image you posted above is not set to initial view controller, select your view controller then from Attribute Inspector check the option available Is Initial View Controller like
you should see an Arrow pointing from you SWRevealVC

Error in passing managedobjectcontext from Tab bar controller to TableviewController using navigationController?

I have a tab bar controller that is connected to a navigationController.
The navigationController is connected to 3 TableviewControllers and the problem I am having is that the managedobject is not passed correctly ; I have it as Nil , the error message is :
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '+entityForName: nil is not a
legal NSManagedObjectContext parameter searching for entity name
'MyEntityName'
Before I added navigationController I had the tab bar controller connected directly to my three TableviewControllers and the managedObject gets passed correctly, any idea what I am doing wrong ?
This is my didFinishLaunchingWithOptions code in AppDelegate :
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
for (id viewController in [tabBarController viewControllers]) {
if ([viewController respondsToSelector:#selector(setManagedObjectContext:)]) {
[viewController setManagedObjectContext:self.managedObjectContext];
}
}
PS: If you need additional peaces of code to understand my issue I'll be glad to post it.
I fixed it by changing my code in didFinishLaunchingWithOptions ; my new code is :
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
NSArray *allTabs = tabBarController.viewControllers;
// Navigation Controller in the tab of BreakFasts
UINavigationController *navigationController = [allTabs objectAtIndex:0];
MyTableViewController *controller1 = (MyTableViewController *)navigationController.topViewController;
controller1.managedObjectContext = self.managedObjectContext;

add a tabbed view as a main view to a navigation-based iphone app

I am relatively new to iOS, hence I apologize for any inconsistency in my question. I need help with the following issue with an app I'm trying to build. My issue is this: The app i am working has a navigation based functionality with a tableview(daily filled by user) and a detailed tableview listing the inputs of the user, but this is just one functionality of the app.
I want to have a main tab based view where one of the tabs(each tab representing a functionality) points to this module.
I wanted to ask for steps and changes i need to make to for example app delegate or rootviewcontroller(I can post the code if it helps better) to make is so that the app starts with a mutli-tabbed bar view where one tab refers to view linked to the rootviewontroller of the navigation-based app.
For summary: Need a main tab bar view where one tab points to the rootviewcontroller highlighted in the screenshot(link below)
If helpful here is a relevant function code i have in app delegate :
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
RootViewController *rootViewController = (RootViewController *)[[navigationController viewControllers] objectAtIndex:0];
rootViewController.managedObjectContext = self.managedObjectContext;
//Next TWO LINES FOR COLOR BACKGROUND
[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundColor:[UIColor redColor]];
}
PS:Here is the screenshot for the storyboard: where i would like to have one tab refer to the view(highlighted in the screenshot) which is linked class rootviewcontroller.m/h
The screenshot: http://i.stack.imgur.com/G9AXI.png
edit: The actual question can be seen as: How and what do i need to do to have a tabbarviewcontroller which i would add with storyboard become my rootviewcontroller instead of the navigationcontroller(highlighted in black in the screenshot: http://i.stack.imgur.com/G9AXI.png).
My current rootviewcontroller.m manages anything related to the tableview of the current navigationviewcontroller, do i need to change that also?.
I apologize for excessiv details, I am really new to iOS dev.
From this one http://i.stack.imgur.com/suLBm.png I tried to embedd in tab barviewcontrol only with storyboard to this one http://i.stack.imgur.com/TZxLo.png I tried to embedd in a tab controller just by story but i get an error :'NSInvalidArgumentException', reason: '-[UIViewController setManagedObjectContext:]: unrecognized selector sent to instance 0x8184e30'
classes related to this are(especially rootviewcontroller.m which is a navigationcontroller for now:
AppDelegate.{h,m}
Configures the Core Data stack and the first view controllers.
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
RootViewController *rootViewController = (RootViewController *)[[navigationController viewControllers] objectAtIndex:0];
rootViewController.managedObjectContext = self.managedObjectContext;
}
RootViewController.{h,m}
Manages a table view for listing all values entered. Provides controls for adding and removing these values.
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = self.editButtonItem;
}
DetailViewController.{h,m}
Manages a detail display for display details of each entered value.
My initial guess is that i need to change the rootviewcontroller appdidfinishlaunching.
Any suggestions ?
In fact now you have:
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
UITabBarController *tabController = (UITabBarController *)self.window.rootViewController;
RootViewController *rootViewController = (RootViewController *)[[[[tabController viewControllers] objectAtIndex:0] viewControllers] objectAtIndex:0];
rootViewController.managedObjectContext = self.managedObjectContext;
}
So you actually need a UITabBarViewController in the Storyboard and you can point to the UINavigationController if you want the ability to push other controllers.
You don't need other UINavigationControllers as I saw in your screenshot, as long as the rootviewcontroller is an UINavigationController.
You can add the UINavigationController as first of the tabs and then you can go and fill the other tabs with the viewcontrollers that you need displayed.
SO basically you need to create UITabBarController as rootviewcontroller.
Let me know if I understood your question correctly.
Here is an example of UITabBarController :
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
//Here you set your controller
UIViewController* centerController = [[UIViewController alloc]init];
UINavigationController *navCenter = [[[UINavigationController alloc] initWithRootViewController:centerController] autorelease];
UITabBarController *tabBarController = [[[UITabBarController alloc] init] autorelease];
tabBarController.viewControllers = [NSArray arrayWithObjects:navCenter,nil];
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.window.rootViewController = tabBarController;
return YES;
}
Let me know if it worked.
You should have something like this :

Parent and child view in Container V

I have a project in iOS and I am trying to modify the same to use in another project. The project is working fine but when I try and Embed the Side View Controller in a tab bar controller it is giving an error
**MFSideMenuDemoStoryboard[23760:c07] -[UITabBarController setLeftMenuViewController:]: unrecognized selector sent to instance 0x757a590
2013-06-13 10:08:51.062 MFSideMenuDemoStoryboard[23760:c07] **** * * **Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITabBarController setLeftMenuViewController:]: unrecognized selector sent to instance 0x757a590'**
i understand that there is something wrong in the Code in appDelegate .m but can't figure out
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:[NSBundle mainBundle]];
MFSideMenuContainerViewController *container = (MFSideMenuContainerViewController *)self.window.rootViewController;
UINavigationController *navigationController = [storyboard instantiateViewControllerWithIdentifier:#"navigationController"];
UIViewController *leftSideMenuViewController = [storyboard instantiateViewControllerWithIdentifier:#"leftSideMenuViewController"];
UIViewController *rightSideMenuViewController = [storyboard instantiateViewControllerWithIdentifier:#"rightSideMenuViewController"];
[container setLeftMenuViewController:leftSideMenuViewController];
[container setRightMenuViewController:rightSideMenuViewController];
[container setCenterViewController:navigationController];
return YES;
}
This is my storyboard
Your root view controller is not a MFSideMenuContainerViewController. It is a UITabBarController. If you want to use MFSideMenuContainerViewController you will want to check out the documentation: https://github.com/mikefrederick/MFSideMenu/
Here is a basic example:
UIViewController *leftSideMenuViewController = [storyboard instantiateViewControllerWithIdentifier:#"leftSideMenuViewController"];
UIViewController *rightSideMenuViewController = [storyboard instantiateViewControllerWithIdentifier:#"rightSideMenuViewController"];
MFSideMenuContainerViewController *container = [MFSideMenuContainerViewController
containerWithCenterViewController: self.window.rootViewController
leftMenuViewController: leftSideMenuViewController
rightMenuViewController: rightSideMenuViewController];
self.window.rootViewController = container;
[self.window makeKeyAndVisible];
This will replace your existing root view controller with a MFSideMenuContainerViewController and place your old root view controller as the center view controller.
As you can see in the storyboard and in the exception, your root view controller is a tab bar controller, not a side menu controller.
UITabBarController has a property viewControllers which gives you access to the controllers inside the tabBarController. You want to get the controller at index 0. This means the viewController at the first tab.
Something like this should work:
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
MFSideMenuContainerViewController *container = tabBarController.viewControllers[0];
// check that container is actually a container and not something else
NSParameterAssert([container isKindOfClass:[MFSideMenuContainerViewController class]]);

Setting the Managed Object Context on a StoryBoard TabBar - Table view Controller with embedded Navigation Controller

I'm trying to use CoreData with Storyboard on a TabBar - NavigationController - TableViewController configuration.
When trying to assign the Managed Object Context to the TableViewController I get
[UITabBarController topViewController]: unrecognized selector sent to instance
If I understand correctly, the problem is that I'm not getting the TableViewController instance. I've tried many things but I don't seem to be getting anywhere.
Your help is much appreciated.
I was able to fixit using Matthijs Hollemans tutorial on raywenderlich.com.
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UINavigationController *navigationController = [[tabBarController viewControllers] objectAtIndex:0];
MyViewController *controller = [[navigationController viewControllers] objectAtIndex:0];
controller.managedObjectContext = self.managedObjectContext;

Resources