I’m currently doing the calculator app that has the graph functionality. So then, I have this button in my Calculator and wired it up on my Calculator2ViewController. Also, I wired this button to another UIViewController named GraphViewController and named the segue’s identifier to be showGraph. Below is the code for my segue.
- (GraphViewController *)graphViewController {
return [self.splitViewController.viewControllers lastObject];
}
- (IBAction)graphPressed {
if ([self graphViewController]) {
[[self graphViewController] setProgram:self.brain.program];
}
else {
[self performSegueWithIdentifier:@"showGraph" sender:self];
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
[segue.destinationViewController setProgram:self.brain.program];
}
There, so no errors and warnings. But when I try to run the app, and press Graph button, the app crashes and get this on my console.
2012-06-18 11:08:17.272 Calculator2[1135:f803] -[Calculator2ViewController graphPressed:]: unrecognized selector sent to instance 0x6c38850
2012-06-18 11:08:17.273 Calculator2[1135:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Calculator2ViewController graphPressed:]: unrecognized selector sent to instance 0x6c38850'
Kindly enlighten me with this as to why is this happening and how can I do this on a better way. Thanks!
EDIT:
Changed - (IBAction)graphPressed to - (IBAction)graphPressed:(id)sender. The segue works now. However, I have this notification on my console (which is kind of scary)
2012-06-18 11:30:02.955 Calculator2[1260:f803] nested push animation can result in corrupted navigation bar
2012-06-18 11:30:03.308 Calculator2[1260:f803] Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.
2012-06-18 11:30:03.309 Calculator2[1260:f803] Unbalanced calls to begin/end appearance transitions for <GraphViewController: 0x6e458d0>.
graphPressedandgraphPressed:are different methods. One has an argument parameter, the other doesn’t. You have implemented it as:But you may have declared the function in the header as:
Which it (rightfully) sees as two completely separate functions. Now, you can actually declare and implement it without any parameters (
-(IBAction)graphPressed) or with parameters (-(IBAction)graphPressed:(id)sender) but what matters is that it is consistent in both declaration and implementation. The declaration and implementation must be the same.Just a tip, it is a best practice to always declare IBActions as
-(IBAction)myButtonWasPressed:(id)senderbecause this lets you know what button was pressed (via thesenderparameter) and it allows you to have multiple buttons that fire off the same event and still be able to tell which was pressed.