I'm getting the following error when trying to set a string in a public property in prepare for segue. Any idea why?
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController setQuestionObjectId:]: unrecognized selector sent to instance 0x7fa713562b40'
The code is:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"postSegue"]) {
CommentsViewControllerNew *commentsVC = (CommentsViewControllerNew *)[segue destinationViewController];
commentsVC.hidesBottomBarWhenPushed = YES;
PFObject * question=[self.brightenArray objectAtIndex:self.indexPathOfClickedpost.row];
commentsVC.questionObjectId=question.objectId;
NSLog(#"%#",[self.Array objectAtIndex:self.indexPathOfClickedpost.row]);
// commentsVC.question = question;
As you may have already gathered, this is caused because you are sending a setQuestionObjectId: message to a class that doesn't recognise that message. E.g. if you send a reloadData (from UITableView) message to an NSString then you will get a similar exception. This is because NSString doesn't implement (have a method) called reloadData.
This error often happens in the prepareForSegue:sender: method because you are typecasting a UIViewController to a custom subclass (in this case CommentsViewControllerNew). The typecasting happens on this line:
CommentsViewControllerNew *commentsVC = (CommentsViewControllerNew *)[segue destinationViewController];
You are essentially telling the compiler: Yes I know that you think this is a UIViewController but it's actually a CommentsViewControllerNew. The crash comes because, in this case the compiler is right, it is a UIViewController and UIViewController doesn't have a method or property name called questionObjectID
After that long explanation... The fix is to go to your Interface Builder, select the relevant view controller and set its class as CommentsViewControllerNew.
NOTE: If you don't understand, or are unfamiliar with any of what I'm talking about, I suggest you do some reading or do a few tutorials. There are loads of good ones on YouTube.
Related
I'm trying to pass data from my ViewController to TabBarController by using Objective-C. I'm trying to assign some data to "bottomTabEventList" (which is a property of my custom TabBarController class). Unfortunately, my program crashes by giving unrecognized selector instance error/warning.
In custom header of TabBarController class, named BottomTabview:
#interface BottomTabView : UITabBarController <UITabBarControllerDelegate>
#property(strong,nonatomic)EventList *bottomTabEventList;
#end
And prepareForSegue method in ViewController.m
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
BottomTabView *btw = [segue destinationViewController];
//checking
if(btw == nil)
NSLog(#"btw in viewController is nil");
else
NSLog(#"btw in viewController is NOT nil");
if(self.eventList.eventList == nil)
NSLog(#"eventList in viewController is nil");
else
NSLog(#"eventList in viewController is NOT nil"); //end of checking
btw.bottomTabEventList = self.eventList; //This is where crash appears
}
Exact crash log is:
-[ViewController setBottomTabEventList:]: unrecognized selector sent to instance 0x7fe923c6ba00
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController setBottomTabEventList:]: unrecognized selector sent to instance 0x7fe923c6ba00'
Segue is from ViewController to BottomTabView and its type is "Present Modally". I'd really appreciate if you can help/guide me. Thanks in advance.
The problem seems to be that btw is not actually of type BottomTabView and is just a UIViewController hence the crash log giving:
[ViewController setBottomTabEventList:]: unrecognized selector sent to instance
As UIViewController does't know what BottomTabEventList is.
You need to make sure that btw is actually a BottomTabView instance.
Do a little introspection and I bet it will not go into this statement:
if ([btw isKindOfClass:[BottomTabView class]]){
btw.bottomTabEventList = self.eventList;
}
I'm trying to call a method before I change the ViewController. Here's my Code:
ViewController.m (First View, here I want to set a String)
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"loginCorrect"]){
[segue.destinationViewController setMail:#"asd"];
}
}
ViewControllerMainMenu.h
- (void)setMail:(NSString*)mail;
#property (strong) NSString *userMail;
ViewControllerMainMenu.m
- (void)setMail:(NSString*)mail
{
self.userMail = mail;
}
As you can see, I want to use the userMail String in the second View, which I get in the first View (a classic Login should be the result).
But I always become this Error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITabBarController setMail:]: unrecognized selector sent to instance 0x147635080'
Hope you can help me, Thanks!
Emanuel
You need to take reference of YourViewController
UITabBarController *tabbar=[segue destinationViewController];
// i am assuming YourViewController at index 0
YourViewController *vc=(YourViewController *)[tabbar.viewControllers objectAtIndex:0];
[vc setMail:#"asd"]
YOu are calling a method on tabbarcontroller which is rely on viewcontroller, so call it properly to reach your goal
It looks like you are sending a message to the object that can not handle this. This is caused because you think you are sending it to the right object but in fact (in runtime) it is not. You should debug and see what is exactly the problem. It might be the retrieving the object you are sending a message or you should have some check if that sending a message should occur in fact - all that depends what you want to achieve. Maybe you will need some casting to help compiler figure out whit what to deal with
I'm guessing your segue destination is a UITabBarController and in it you have a custom UIViewController? We'll need to see proper code in order to answer properly but here's my best guess for now.
First you need to import your class...
#import "ViewControllerMainMenu.h"
Then in prepareForSegue...
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"loginCorrect"]){
UITabBarController *tabBarController = segue.destinationViewController;
ViewControllerMainMenu *controller = tabBarController.viewControllers[0];
controller.mail = #"asd";
}
}
Obviously, this depends on exactly how everything is set up but you should be able to adapt it from here.
I'm getting this error when passing values from one viewcontroller to the next:
"unrecognized selector sent to instance 0x1f5ea840"
"'NSInvalidArgumentException', reason: '-[UIViewController setContainerToLocationFromResultVC:]: "
I've created a strong property in the 2nd VC and it seemed to work well until I made some modifications to use Container View.
Here's my code:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:#"resToContainerSegue"]) {
containerViewController *containerVC= segue.destinationViewController;
containerVC.container_toLocationFromResultVC=self.toLabel.text; // also tried "toLabel.text" but no use.
containerVC.container_fromLocationFromResultVC=self.fromLabel.text;
}
}
Please let me know if I need to provide any more specific.
I'd be very glad for any help.
Thanks in advance
I got the answer: My destination viewController was not pointing to the proper class. Thanks everyone for helping :)
I have everything implemented, and the following method that fires when I return to the source view controller:
- (IBAction)returned:(UIStoryboardSegue *)segue {
...
}
I want to take the value from the UITextField in the view I'm returning from, and set a value in my source view (or the view that calls this method) to the value of that UITextField.
I tried this:
- (IBAction)returned:(UIStoryboardSegue *)segue {
AddTextViewController *returnedFromViewController = segue.destinationViewController;
NSString *inputtedText = returnedFromViewController.textField.text;
self.foo = inputtedText;
}
But I get this error:
[RootViewController textField]: unrecognized selector sent to instance 0x8dc0250
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[RootViewController textField]: unrecognized selector sent to instance 0x8dc0250'
What am I doing wrong in that code above? There's hardly any documentation on this, so it's very hard to search around myself.
You're using the wrong controller. You want the segue's sourceViewController, not the destinationViewController. Otherwise, you're doing it all correctly.
The best way to do this is to implement a protocol / delegate. The delegate will handle any requests from your destination view controller and send the data back to your source view controller.
http://iosdevelopertips.com/objective-c/the-basics-of-protocols-and-delegates.html
I hope someone can help with this.
I have a UITableViewController and want to pass a value to a UIViewController called NewsArticleViewController when the tablecell is selected. I've created a segue from the tablecell to the view controller.
When I call my prepareForSegue method below:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:#"ShowNewsArticle"])
{
NSIndexPath *indexPath = [self._tableView indexPathForSelectedRow];
NSDictionary *article = [_articles objectAtIndex:indexPath.row];
NSString *articleID = [article valueForKey:#"id"];
NSLog(#"Trying %#", articleID);
NewsArticleViewController *detailViewController = [segue destinationViewController];
detailViewController.articleID = articleID;
}
}
The NSLog shows the NSString value correctly before the error occurs on the last line.
I get the error:
2012-12-11 23:08:41.915 My School[4689:c07] -[UIViewController setArticleID:]: unrecognized selector sent to instance 0x8088140
2012-12-11 23:08:41.916 My School[4689:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController setArticleID:]: unrecognized selector sent to instance 0x8088140'
*** First throw call stack:
(0x1800012 0x11c5e7e 0x188b4bd 0x17efbbc 0x17ef94e 0x3ca1 0x554ac7 0x554b54 0x1bc899 0x1bcb3d 0xbc3e83 0x17bf376 0x17bee06 0x17a6a82 0x17a5f44 0x17a5e1b 0x1cc87e3 0x1cc8668 0x10d65c 0x1f4d 0x1e75 0x1)
libc++abi.dylib: terminate called throwing an exception
On the destination view controller, NewsArticleViewController, I have declared this in the header:
#property(strong,nonatomic) id articleID;
And I have synthesized the property in the method. I'm using ARC, I don't know if this is the specific cause but I can't proceed until I sort this out. Thanks.
In your error message, UIViewController is reporting the "unrecognized selector" error. I suspect your storyboard has not specified your custom NewsArticleViewController for this scene. Thus, it's using the default UIViewController which obviously doesn't understand the setArticleID.
Check the "Custom Class" setting for the view controller in Interface Builder:
If the custom class has not specified, it will look like the above screen snapshot. Just fill in the class name.