3

Anyone know why I am getting this error?

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CustomRaisedTabViewController cancel:]: unrecognized selector sent to instance 0x4d321e0'

This is the code where it is failing. This is in my CustomTabViewController. The error is happening when I click my "Cancel" button.

-(IBAction)showPostModalViewController {

PostActionModalViewController *addController = [[PostActionModalViewController alloc] 
                                                initWithNibName:@"PostActionModalView" bundle:nil];

// Configure the PostAddViewController. In this case, it reports any
// changes to a custom delegate object.

addController.delegate = self;



// Create the navigation controller and present it modally.

UINavigationController *navigationController = [[UINavigationController alloc]
                                                initWithRootViewController:addController];

[self presentModalViewController:navigationController animated:YES];

UIBarButtonItem *cancelButton =
[[UIBarButtonItem alloc] initWithTitle: @"Cancel"
                                 style: UIBarButtonItemStylePlain
                                target: self
                                action: @selector(cancel:)];
addController.navigationItem.rightBarButtonItem = cancelButton;
[cancelButton release];


//[self presentModalViewController:addController animated:true];
[navigationController release];

[addController release];
}

-(IBAction)cancel {
    [self.parentViewController dismissModalViewControllerAnimated:YES];
}
sudo rm -rf
  • 29,408
  • 19
  • 102
  • 161
jdog
  • 10,351
  • 29
  • 90
  • 165
  • 1
    You're trying to call a selector that has an argument, while your actual method doesn't have an argument. Just remove the colon after `cancel:` in your `@selector` and it should work fine. Alternatively, you can modify your `cancel` action as jer noted below. – sudo rm -rf Feb 26 '11 at 17:24
  • Possible duplicate of [How can I debug 'unrecognized selector sent to instance' error](https://stackoverflow.com/questions/25853947/how-can-i-debug-unrecognized-selector-sent-to-instance-error) – Cœur Jul 08 '19 at 05:50

3 Answers3

9

Because the cancel: method is not cancel which is what you've defined.

Change your cancel action to look like this:

- (IBAction)cancel:(id)sender {
    ...
}
jer
  • 20,094
  • 5
  • 45
  • 69
  • I am a noob... When I cancel... will it know to close/destroy both the addController and the navigationController – jdog Feb 26 '11 at 19:20
  • No guarantees that it'll occur right away. But assuming you follow the memory management rules then at some point, when the system sees fit, sure. – jer Feb 26 '11 at 19:21
  • 1
    Wouldn't it have been simpler just to delete the extraneous colon in the `@selector` directive? The OP's `cancel` method would be ignoring the argument anyway, so why pass it? – jlehr Feb 26 '11 at 20:15
  • By convention actions take in an argument of the sender, if you start going off convention it's easy to start getting things wrong as sometimes you use the sender and sometimes you don't. Consistency in use over time and between developers eliminates a lot of potential for error. – Kendall Helmstetter Gelner May 24 '14 at 19:40
1
action: @selector(cancel:) 

For the action selector which takes parameter! cancel: that means which will take another parameter.

change your method to

-(IBAction)cancel:(id)sender{
// Do wat you want
}

or

-(IBAction)cancel:(UIButton *)btnSender{
/// Do what you want
}
Kiran
  • 345
  • 1
  • 4
  • 16
0

you have to modify the cancel method signature in

-(IBAction)cancel:(id)sender
 { 
   [self.parentViewController dismissModalViewControllerAnimated:YES]; 
 }

when you added the action to your cancelButton (during initialization) you specified the "cancel:" selector, this means that it will be called a method having one parameter (the sender button)

Sangram Shivankar
  • 3,535
  • 3
  • 26
  • 38
scriba
  • 97
  • 5