Why does inserting navigation create unrecognized selector sent? - ios

On my app I decided to change my segue to a Show Details segue in the story board as I wanted the affect of the storyboard coming from the bottom of the screen, and thus I needed some way to bring the user back, so I inserted a navigation into the destination view. However now I get:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationController setImageData:]: unrecognized selector sent to instance 0x7fbdfa887600'
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:#"infoSegue"])
{
// Get reference to the destination view controller
DetailsViewController *vc = [segue destinationViewController];
// Pass any objects to the view controller here, like...
int num = (int)[self getIndexNum];
NSString *dictionaryKey = _results[num];
Image *image = _dataDictionary[dictionaryKey];
vc.imageData = image;
NSLog(#"Vendor ID %#",image.vendorId);
}
}
Has the navigation become the parent of the view or something?

As destinationViewController returns the generic type UIViewController you have to do a type cast.
DetailsViewController *vc = (DetailsViewController *)[segue destinationViewController];

if your destination view controller is a navigation controller, then you shouldn't set the image on navigation controller!! Also as #vadian from the answer said, you need to cast the return type of uiviewcontroller!!!
UINavigationController *navController = [segue destinationViewController];
DetailsViewController *viewController = (DetailsViewController *)([navController viewControllers][0]);
[viewController setimageData:image];
Make sure imageData is of kind UIImage. If it is of type NSData, you might need to do:
[viewController setimageData:[UIImage imageWithData:imageData]];

Related

From view controller to PageViewController

i've got a UIViewControllers with two UIButtons, i want to know how to pass from this UIViewController to my UIPageViewController,
i tried to link the UIButton to my UIPageViewController from the storyboard but it gives me this error
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: 'Storyboard () doesn't contain a view controller with identifier
'Guide''
how should i do?
EDIT
- (IBAction)goToGuide:(id)sender{
[self performSegueWithIdentifier:#"goToGuide" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"nextView"])
{
FirstLaunch *fLaunch = [segue destinationViewController];
//pass any data to next view here
}
}
Added this to my UIViewController and added the identifier in my storyboard a connection between the view controller and the UIPageViewController and set the identifier to goToGuide, but same error..
UP
EDIT2
- (IBAction)goToGuide:(id)sender{
[(UINavigationController*)self.window.rootViewController pushViewController:Guide animated:YES];
[self performSegueWithIdentifier:#"NextView" sender:self];
}
It says, property windows not found on object of type FirstLaunch
Give the segue identifer in the storyboard, you must have missed it.
Check this answer
ios automatically segue from one screen to another

[UITabBarController setSports:]: unrecognized selector sent to instance 0x7b80e2b0

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;

Passing data from PFQueryForTableViewController cell through segue to viewController

so I am trying to pass data from my cell (populated with queryForTable method) through a segue to another viewController. I am using the prepareForSegue method and my code looks like:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"profile"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
PFObject *object = [self.objects objectAtIndex:indexPath.row];
//UINavigationController *nav = [segue destinationViewController];
ProfileViewController *userProfile = [segue destinationViewController]; //(ProfileViewController *) nav.topViewController;
userProfile.userInfo = object;
}
}
in my VC I have the property defined:
#property (weak, nonatomic) PFObject *userInfo;
but when I run my code I get the error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController setUserInfo:]:
It looks like the viewController you're pulling out of the segue isn't a ProfileViewController - the error you're getting states:
-[UIViewController setUserInfo:]:
Note UIViewController, not ProfileViewController. This would seem to indicate that your segue's destination viewController isn't exactly what you think it is.
Check your destination viewController's class in your Storyboard. Is it set to ProfileViewController? Is your ProfileViewController embedded inside another viewController (like a navigationController - looking at your commented out code, you seem to have treated it as such before)? If so, you might need to do a big more digging to get a reference to the correct viewController.

When I use prepareForSegue to pass value to the aim controller, there are some problems

Firstly, I use storyboard to create two uiviewcontroller: firstviewcontroller and secondviewcontroller. And using xcode to embed a uinavigationcontroller into secondviewcontroller(editor --> embed in --> navigation controller). Then, I also want to use segue to pass value from first to second. there is the code:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"passValue"])
{
secondViewController *saveViewController = [segue destinationViewController];
[saveViewController setValue:data forKey:#"data"];
}
}
But there will report error, the log is:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key data.'
However, when I delete the navigation controller that I use Xcode to insert it and define the navigation bar by code, this problem will disappear. I guess the reason is the real destinationViewController is navigationcontroller instead of secondviewcontroller. So if I want to reserve the navigation controller that is embedded by Xcode, how can fix this problem?
You're right, destinationViewController will be a UINavigationController you have secondViewController embeded in. Maybe try that:
secondViewController *saveViewController = [[segue destinationViewController] topViewController];

How can I display image data in next view by using Storyboards in iOS [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I pass image ID to next view by using Storyboards in iOS
My code is:
- (IBAction) buttonPressed:(id)sender
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle: nil];
DestinationViewController *destinationViewController = [storyboard instantiateViewControllerWithIdentifier:#"Destination"];
destinationViewController.title = #"image property";
[self.navigationController pushViewController:destinationViewController animated:YES];
[destinationViewController release];
}
But i got error is:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Storyboard (<UIStoryboard: 0x68a0840>) doesn't contain a view controller with identifier 'Destination''
First throw call stack:
(0x13d4022 0x1565cd6 0x448fef 0x39f6 0x13d5e99 0x2114e 0x210e6 0xc7ade 0xc7fa7 0xc7266 0x463c0 0x465e6 0x2cdc4 0x20634 0x12beef5 0x13a8195 0x130cff2 0x130b8da 0x130ad84 0x130ac9b 0x12bd7d8 0x12bd88a 0x1e626 0x2902 0x2875)
terminate called throwing an exception(lldb)
Any thoughts?
Thanks in advance.
You need to set the view controller's identifier to "Destination" in IB. The following iextracted from Apple's document on UIStoryBoard.
An identifier string that uniquely identifies the view controller in the storyboard file. You set the identifier for a given view controller in Interface Builder when configuring the storyboard file. This identifier is not a property of the view controller object itself and is only used by the storyboard file to locate the view controller.
The answer involving setting the identifier correctly is probably your problem. However, it seems like you're doing this in a kind of hacky way by getting a reference to the storyboard and having it instantiate the controller.
Instead, try creating a push segue between the view controller and the DestinationViewController in the storyboard editor and do this:
- (IBAction)buttonPressed:(id)sender {
[self performSegueWithIdentifier:#"yourSegueIdentifierHere"];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
DestinationViewController *controller = [segue destinationViewController];
[controller setImage: ... ];
}
You can use the
(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
method to pass data between view controllers using
myViewController *viewController = [segue destinationViewController];
to specify your destination.

Resources