I'm trying to follow along the Stanford CS193p iOS programing lectures. One of the demo programs, called "Happiness" creates two UIViewControllers, a "PsychViewController" and a "HappinessViewController." It segues from the PsychViewController to the HappinessViewController using a target action method.
The following code keeps throwing this exception: "-[UIViewController setHappiness:]: unrecognized selector sent to instance"
Here's the offending code:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"ShowDiagnosis"]) {
[segue.destinationViewController setHappiness:7];
}
}
I have searched this site and others, and the usually when this error comes up, it is because the generic UIViewController has not been correctly set to the specific view controller object used in the program, in this case the "HappinessViewController." But I have set the generic UIViewController to be a HappinessViewController using the identity inspector in IB, and I am still getting the exception. I am tearing my hair out, if anyone could help it would be much appreciated.
Let's look at the exception:
-[UIViewController setHappiness:]: unrecognized selector sent to instance
This tells you two things about the message that was unrecognized. It tells you the selector, setHappiness:. It also tells you the actual, runtime class of the receiver, UIViewController.
Of course, UIViewController has no method named setHappiness:. That's why you got the exception.
You wrote a subclass of UIViewController called HappinessViewController which does have a setHappiness: method (or a read/write property named happiness, which generates the method). And you intended for the receiver (the destination view controller) to be an instance of that subclass (HappinessViewController).
But the exception is telling you that the destination view controller is just a plain UIViewController. So even though you think you did, you probably did not set the view controller's custom class in the storyboard. Maybe you set the custom class of some other view controller, but you didn't set the class of this segue's destination.
You need to set the destination view controller's custom class in the Identity Inspector, like this:
I figured out the problem. Although I had correctly specified that the relevant UIViewController was a HappinessViewController, the linker, was for some reason, not linking to the correct files. The fix was to go to double click on the .xcodeproj file inside Xcode, then go to Build Phases and manually add the files under "Compile Sources."
For me changing the class of the view controller in the story board worked.
Try like this:
HappinessViewController *controller = segue.destinationViewController;
[controller setHappiness:7];
I had the same problem today and it was because my custom class was in a library. The library itself was being linked in build phases, but that in itself was not enough to pull in the class. So finally I solved it by add the following line to my AppDelegate.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[ CustomClass class ];
}
This forces the linker to pull in the class. Otherwise simply linking in the library may not be enough to pull in the class unless it is referenced somewhere in the application.
I tried adding a Cast and it worked for me, I had the same problem:
FirstViewController *detailViewController =
(FirstViewController *)[segue destinationViewController];
Check which prepareForSegue is triggering. My problem was that 2 segues were triggering, in the current viewController and in the incoming viewController. The solution, and always a good practice is to check segues identifiers.
- (void)prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"segueIdentifier"]) {
}
}
Cast before assigning
HappinessViewController *controller = (HappinessViewController *)segue.destinationViewController;
[controller setHappiness:7];
Make sure in your storyboard, HappinessViewController is set as the class of you VC
Related
I perform a segue which is defined in the Storyboard to open a new view controller. I need to configure the destination view controller of the segue in a special state where some of it's buttons does not needed to be displayed.
I know that I can do this by setting a variable on this controller in my source view controller's -prepareForSegue:sender:. The problem with this is, that firstly it instantiates the controller, so it's -viewDidLoad: will run, then only after can I set anything on it.
I can't create the controller entirely from code, because it's user interface is in Storyboard. -instantiateViewControllerWithIdentifier: also calls -viewDidLoad first obviously.
I could probably use a semaphore and add the initialization code into my destination controller's -viewWillAppear, but that's ugly, it has to be some more elegant way to do this than doing a check every time the view appears. (My settings need to be done only once.)
Is there some way to pass variables into the controller before it's -viewDidLoad runs?
EDIT: It looks like this happens only if I trigger the segue from code using -performSegueWithIdentifier:.
On my machine and on iOS 8.0 and iOS 9.0, viewDidLoad is called after prepareForSegue. So, something like the following worked for my test case of your answer.
In your source controller:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
TimViewController * controller = segue.destinationViewController;
if( [controller isKindOfClass:[TimViewController class]] )
controller.name = #"tim";
}
In your destination controller (TimViewController):
- (void)viewDidLoad
{
[super viewDidLoad];
// Do view setup here.
NSLog( #"view did load %#", self.name );
}
Add a segue (a show segue) from your source control to the destination view controller.
Output:
2015-09-17 19:09:04.351 Test[51471:7984717] view did load tim
I think there is some confusion here. -prepareForSegue:sender: gets called before -viewDidLoad gets called. Please double check your implementation.
Edit:
May be this thread will help you understand this and one of the mentioned cases fall in your case.
I have two viewcontrollers mainVC and drop_downVC. I also have an object called JSONutils. The user will enter input into a textfield in the mainVC viewcontroller and methods are then used in the JSONutils to contact a RESTful api and a JSON object is then returned and parsed. I'd like to segue to the drop_downVC viewcontroller when the connectionDidFinishLoading method in JSONutils completes. Everything is working up to the point of trying to segue to the drop_downVC. From the research I've been doing it seems using the performSegueWithIdentifier is the way to go. So below is the code that I'm trying to implement in the connectionDidFinishLoading method in JSONutils.
//JSONutils.m
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//NSLog(#"Succeeded! Received %d bytes of data",[receivedData length]);
//NSLog(#"The receivedData is: %#", receivedData);
//JSON parsing code from here to
//here...
[self performSegueWithIdentifier:#"segue_drop_downVC" sender:self];
}//(void)connectionDidFinishLoading
the above code gives me the following error.
No visible #interface for 'JSONutils' declares the selector 'performSegueWithIdentifier:sender:'
I've googled this error but can't find anything that applies to my specific solution.
I created the segue and named the segue identifier appropriately (segue_drop_downVC) using this post.
I've also been putting the following code anywhere (JSONutils, mainVC) possible to see if that would change anything but it hasn't.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
//NSLog(#"The segue identifier is: %#", [segue identifier]);
//if ([[segue identifier] isEqualToString:#"YOUR_SEGUE_NAME_HERE"])
drop_downVC *nextViewController = [segue destinationViewController];
}
I don't have much experience programming in Objective-C but when I have I've used storyboard and segue to move between viewcontrollers. When I have used storyboard and segues I've "segued" when the user hit a button. I've never had to wait for data to come back and then navigated to the next viewcontroller. My plan is continue to put all of my JSON manipulating and parsing methods in the JSONutils files. So I'd like to keep those type of methods out of mainVC and drop_downVC. I've been doing research, trying different things but can't seem to get it. Any help or feedback is greatly appreciated! Thanks.
The performSegueWithIdentifier is a method of the UIViewController class, so if your JSONUtils class is not a subclass UIViewController, you cannot send that message to self. That's what the error says.
The same applies to prepareForSegue.
To solve your problem, there are several alternatives. Maybe the simplest one is to use delegates.
You need to call performSegueWithIdentifier from your view controller, not JSONutils. I think the way to do this is to create a delegate protocol in JSONutils and have the MainVC set itself as delegate when it creates an instance of JSONutils. Call the delegate method from connectionDidFinishLoading:
I really haven't done much and i'm already stuck.
So far i've done:
added NSWindowController subclass (MikesWindowController.h & .m)
removed windowNibName from MikesDocument.m (since i'm implementing my own
WindowController subclass.)
I tried:
Tested if NSLog would come back at init, windowControllerDidLoadNib, applicationDidFinishLaunching. Only the NSLog at init printed.
And, tested the Main Menu -> File -> New after after compiling my Document app.
Am I implementing this right? Thanks. Any suggestions would be great! Under MikesDocument.m
-(void)makeWindowControllers{
MikesController *controller = [[MikesWindowController alloc]init];
[self addWindowController:controller];
}
After much deliberation I found that answer. Woohoo. Enjoy future.
removed initWithWindow from my NSWindowController subclass
implemented initWithWindowNibName to my NSWindowController subclass so now anytime I initialize I must specify the window nibName.
Below I implemented initWithWindowNibName in my NSWindowController subclass heres what it looks like:
MikesWindowController.m
-(id) initWithWindowNibName:(NSString *)windowNibName{
self = [super initWithWindowNibName:windowNibName];
return self;
}
(Below) Back again to the main document I corrected the makeWindowController method and instantiated my controller with "MikesDocument" (for MikesDocument.xib) and added it.
MikesDocument.m
-(void)makeWindowControllers{
MikesWindowController *controller =
// must tell controller which nib file to use.
[[MikesWindowController alloc]initWithWindowNibName:#"MikesDocument"];
[self addWindowController:controller];
}
Success! Don't even bother calling init or implementing init as it returns an error at any time.
I am trying to build a 2 scene application using Storyboards in iOS6.
I am taking the users name via text input in the first scene and passing it using a push segue to the second scene; where it is displayed in a label.
The first scene's UIViewController is called ViewController and the second scenes UIViewcontroller is DrawViewController.
I have imported the the DrawViewController.h in my ViewController.m file where I have defined the prepareForSegue as below:
-(void) prepareForSegue:(UIStoryboardSegue *) segue sender:(id) sender{
if ([segue.identifier isEqualToString:#"ColorPickerControllerSegue"]){
DrawViewController *dvc =[segue destinationViewController];
dvc.userName= self.userName;
}
}
Here userName is a NSString defined in DrawViewController.
I am getting "use of undeclared identifer:DrawViewController".
I am quite new to iOS programming, so is there something I am missing here?
I have set the second view controller's custom class to DrawViewController.
I was able to solve this issue by removing the existing DrawViewController.h and DrawViewController.m files and then creating and adding them back to the the project.
It seemed to do the trick.
I'm stuck with the following. In a program, I'm trying to communicate between different classes (View Controllers with NIB files attached in a TabBar application etc). I want to call a method 'OMFG' in a class called 'ProductViewDetailController'. This class is a UIViewController (SplitViewDelegate). It's loaded programmatically.
Anyways, I've been trying to get the right call to this controller, and I came up with 2 solutions. One is declaring the productviewdetailcontroller in the caller's .h file and .m file, making an IBOutlet, linking it in the Interface builder and calling it directly by the line
[productDetailController OMFG];
When I call this method, it calls the right method in the ProductViewDetailController, but the instance of this viewcontroller differs from the one I programmatically can reach with this code:
for (UIViewController *controller in self.tabBarController.viewControllers) {
NSLog(#"%#", [controller class]);
if ([controller isKindOfClass:[UISplitViewController class]]) {
UISplitViewController *cell = (UISplitViewController *)controller;
for (UIViewController *controller2 in cell.viewControllers) {
NSLog(#"%#", [controller2 class]);
if ([controller2 isKindOfClass:[ProductViewDetailController class]]) {
[controller2 OMFG];
}
}
}
Which one should I use, and why?
edit: When I try to add a SubView to both viewcontrollers, the one where the call is [controller2 OMFG]; actually shows the newly added view, where the [productDetailController OMFG]; doesn't show the newly added view... Why is that? Is there a shorter (and more chique) way to get access to the right ViewController?
You should use a IBOutlet. This makes sure your app can still call the correct target if you later decide to change the hierarchy of view controllers, for example if creating an iPhone compatible setup without a UISplitViewController.
Calling isKindOfClass: in Objective-C is a sure sign that what you are doing is probably wrong. Firstly in Cocoa Touch what you do is always more important than who you are. Secondly what you try to do is probably peeking inside something that should be left private.