I know there are lots of postings about this but I have tried everything and nothing has worked. So I have tried to pass an object between two view controllers, to a DBKIngredientsViewController embedded in a navigation item. I have a push segue with the identifier "showIngredientsSegue" to the DBKIngredientsViewController. The error message I receive is:
'NSInvalidArgumentException', reason: '-[DBKIngredientsViewController topViewController]: unrecognized selector sent to instance 0x8a92450'
The view controller to which I am segueing is embedded in a navigation controller, which I think is messing it up. What's the way around this? To be clear, the DBKViewController is already embedded in a navigation controller, and the push segue pushes the DBKViewController, not the navigation controller embedding it. I have tried it different ways but none seem to work.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"showIngredientsSegue"]){
UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
DBKIngredientsViewController *controller = (DBKIngredientsViewController *)navController.topViewController;
controller.targetRecipe = selectedRecipe;
}
}
Are you sure that segue.destinationViewController is a UINavigationController? It seems like it's just a DBKIngredientsViewController so this should work:
DBKIngredientsViewController *controller = (DBKIngredientsViewController *)segue.destinationViewController
Also if DBKViewController already has a navigation controller then you do not need a second one if you are pushing DBKIngredientsViewController. You would only need a second one if you are modally displaying DBKIngredientsViewController.
Related
I have a segue from a button in a detail view controller to a new modal view controller that displays a map.
To pass the location to display on the map, I use preparetosegue as follows:
if ([segue.identifier isEqualToString:#"showMap"]) {
IDMapView *destViewController = segue.destinationViewController;
destViewController.contact=_contact;
}
However, I after embedding the modal view controller in a navigation controller to implement a cancel button, the app is now crashing when I click on the button giving the error in the log:
[UINavigationController setContact:]: unrecognized selector sent to instance 0x7f8738ea5ce0
(lldb)
The error seems to have something to do with the navigation controller not having the property contact (which is on the view controller.)
Can anyone suggest the right way to do this?
The destinationViewController is now a UINavigationController and not an IDMapView. You need to get the topViewController from the UINavigationController.
if ([segue.identifier isEqualToString:#"showMap"]) {
UINavigationController *navigationController = segue.destinationViewController;
IDMapView *destViewController = navigationController.topViewController;
destViewController.contact=_contact;
}
I’m building an iOS app. I want pass array value from one view controller to another through segue.
While doing this i’m getting an error:
[UITabBarController setSports:]: unrecognized selector sent to
instance 0x7b80e2b0.
here is my code:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"sportsSelection"]) {
Play *play=[segue destinationViewController];
play.sports=selectedSports;//error break point is here.
//sports and selected sports are NSMutableArray
}
}
To make the situation a bit clearer, you are getting the error because you are trying to call a method (setSports:) on a class that does not implement this method, this is exactly what the error message tells you:
[UITabBarController setSports:]: unrecognized selector sent to
instance 0x7b80e2b0.
Your segue apparently has a UITabBarController as destination, so it's clear that it does not know about the method setSports:, since this one is actually implemented in your custom view controller (apparently called Play?!).
Then, as Eike pointed out in his answer, you need to get Play from the UITabBarController which it is embedded in. That's why he suggested to use: Play* p = ((UITabBarController*)segue.destinationViewController).viewControllers[0];, it means that you should get the view controller at index 0 from your UITabBarController.
According to your comment, the view controller at index 0 is a UINavigationController, which (naturally) also does not respond to setSports:, because just like UITabBarController it is a class provided by Apple and doesn't know about this method.
Now, you need to find out where in this UITabBarController your custom view controller Play is located. Either it is a direct part of UITabBarController and you can find it by using Eike's approach and just modify the index from 0 to n (where n is the number of view controllers that the UITabBarController has hold of), or another option is that it is embedded in the UINavigationController that you received at index 0, so in that case you'd have to access the UINavigationController's view controller stack (e.g. the array property viewControllers or just the one that is currently on top of the stack using topViewController).
Edit: I want to give you some extra information about what's going on in your code, especially related to Eike's answer:
From the information that you gave us in your question and in the commment to Eike's solution, we can assume the following code to be correct:
if ([[segue identifier] isEqualToString:#"sportsSelection"]) {
UITabBarController *tabBarController = [segue destinationViewController]; // the destination of the segue is your `UITabBarController`
UINavigationController *navigationController = tabBarController.viewControllers[0]; // gets the first of the view controllers contained in your UITabBarController
NSLog(#"view controllers in navigation controller: %#; top view controller: %#", navigationController.viewControllers, navigationController.topViewController); // print all view controllers managed by navigationController
}
EDIT 2: From your comment I can now assume the following code to be correct:
if ([[segue identifier] isEqualToString:#"sportsSelection"]) {
UITabBarController *tabBarController = [segue destinationViewController]; // the destination of the segue is your `UITabBarController`
UINavigationController *navigationController = tabBarController.viewControllers[0]; // gets the first of the view controllers contained in your UITabBarController
Play *controller = (Play *)[[navigationController viewControllers] objectAtIndex:0];
controller.sports=selectedSports;
play.sports = selectedSports;
}
You need to setSports: to your view controller not the UITabBarController.
Play* p = ((UITabBarController*)segue.destinationViewController).viewControllers[0];
p.sports = selectedSports;
See the following setup in my Storyboard:
I've got two container views in the first view controller, one for the main view that displays posts, and the other for a menu that the posts view slides out to reveal.
I'd like the menu bar to slide away like described here, which I have working but without the navigation bar. I've tried using Editor > Embed in Navigation Controller to both the view controller that contains the children (which worked, but when the menu slid out it still showed for the menu which doesn't look as intended) and when I just do it for the posts view controller it crashes with this error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationController setContainingViewController:]: unrecognized selector sent to instance 0x8e81c60'
Does anyone know how to accomplish this effect?
I assume this post is related to this one by Doug Smith, Why does hiding my status bar completely break my simple animation?. Your problem doesn't have anything to do with only one of the controllers being embedded in a navigation controller. It crashes, because you're trying to call setContainingViewController on a UINavigationController instead of the PostsViewController, which was (before you added the navigation controller) the destinationViewController of the embed segue. Since the navigation controller is now the destinationViewController, you need to change the code in prepareForSegue to this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"Posts"]) {
UINavigationController *nav = (UINavigationController *)segue.destinationViewController;
PostsViewController *postsViewController = (PostsViewController *)nav.topViewController;
postsViewController.containingViewController = self;
}
if ([segue.identifier isEqualToString:#"Menu"]) {
MenuViewController *menuViewController = (MenuViewController *)segue.destinationViewController;
menuViewController.containingViewController = self;
}
}
I am having a problem with this.
In my root view controller I am having a textfield & one button. I am giving a condition like if i entered 0 in textfield then only it should move to next view.
upto here it is working correctly. But now here is problem. Here I am having one button & given navigation to third view controller for that button. here i am getting error as
Terminating app due to uncaught exception 'NSGenericException', reason: 'Push segues can only be used when the source controller is managed by an instance of UINavigationController.'
image ref: http://img12.imageshack.us/img12/8533/myp5.png
and i am giving action for button in first view as below
- (IBAction)Submit:(id)sender {
if([tf.text isEqual:#"0"])
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
SecondViewController *vc2 = [storyboard instantiateViewControllerWithIdentifier:#"SecondViewControllerID" ];
[self presentViewController:vc2 animated:YES completion:NULL];
}
}
Go To:
Editor--> Embed In --> Navigation Controller, to add Navigation Controller to your initial view
After looking at your screenshot, you either need to
Push your SecondViewController onto the existing navigation controller's stack instead of presenting it modally OR
You need to embed your SecondViewController in another navigation controller and then create and present that navigation controller modally from your SamplesViewController
Either way, SecondViewController needs to be embedded in a navigation controller before you can use Push Segues from it
Either embed your view controller in a Navigation controller or if there is a Navigation controller in the story board mak it the initial view controller
I also faced same problem. My problem was I was using model segue style (rather that push) for one before the current controller because of that I think it broke the Navigation chain before already.
Embed your view controller in a UINavigationController is a good option if there is no UINavigationController in the storyboard. Otherwise you can select the UINavigationController and select the root view controller in the inspector pane, then drag it to the UIViewController you want to use as root. the screenshot is as below:
I solved this by creating a custom UIStoryboardSegue between the UINavigationController and the destination view controller:
-(void) perform
{
assert([self.sourceViewController isKindOfClass:[MyNavigationController class]]);
MyNavigationController *launchController = (MyNavigationController *) self.sourceViewController;
[launchController setViewControllers:#[self.destinationViewController] animated:YES];
}
Try this. It works for me.when you set root view controller to first view and use self.presentViewController ,controller move to next view but instance of navigation controller is only for first view,third view required navigation instance so use self.navigationController instead of presentViewController.
NSString * storyboardName=#"Main";
UIStoryboard *storybord=[UIStoryboard storyboardWithName:storyboardName bundle:nil];
SecondViewController *vc=[storybord instantiateViewControllerWithIdentifier:#"SecondViewControllerID"];
[self.navigationController pushViewController:vc animated:YES];
I still haven't grasped this transfer with the structure below. I have read many posts, and have seen the same unanswered post by others, but no resolution.
I will try to simplify the question to make it easier for all.
The structure of the project is:
UITabbar with tab1 and tab2
Tab1 has a Nav controller-->ViewController1
Tab2 has a Nav controller -->ViewController2
In viewcontroller1 (tab1) I have object X.
In ViewCOntroller2 (tab2) I want to display object X.
Don't worry about displaying, that's the easy part.
Question: How do you pass object X from tab1 to tab2. (what is the general pattern).
If you want to do it using prepareForSegue, is this ok, or is there a better way.
If using prepareForSegue, where do you drag the segue to?
The tabbarcontroller
OR*****
2. to the second VC
Hopefully this is clear enough. With this in mind how would you perform the transfer?
Using the segue 1:
I tried doing this:
//(From View controller 1)
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"CreateObjectXToDisplayObjectX"])
ViewController2* vc2 = [[ViewController2 alloc] init];
UITabBarController* tbc = [segue destinationViewController];
vc2 = (ViewController2 *)[[tbc customizableViewControllers] objectAtIndex:1];
//Crash here with with [MainNavigationControllerDesign setViewController1Delegate:]: unrecognized selector sent to instance 0x1064ef70'
vc2.viewController1Delegate=self;
vc2.objectXAtViewController2 = _objectXFromViewController1;
}
}
So, how is this Object X transfer accomplished?
Thank you in advance
You don't want to use segues in this way. Segues always instantiate new controllers when you go to them, but you already have these controllers embedded in the tab bar controller. If you were setting this up in code, I would say use a delegate, but if you set this up in IB, it's hard to do that. From VC2, you can get a reference to VC1's navigation controller with self.tabBarController.viewControllers[0]. VC1 will be that navigation controller's topViewController, so, putting that together, and adding a cast, you can access VC1 like this:
ViewController1 *vc1 = (ViewController1 *)[self.tabBarController.viewControllers[0] topViewController];
Once you have that reference, you can access any of vc1's properties. Don't forget to import ViewController1.h into ViewController2's .m file.