Unrecognized selector sent to instance

Does 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 in which it does not work. It is in mine CustomTabViewController. An error occurs when I click the 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];
}
+3
source share
3 answers

Because the method is cancel:not the cancelone you defined.

Modify the action cancelso that it looks like this:

- (IBAction)cancel:(id)sender {
    ...
}
+9
source
action: @selector(cancel:) 

For an action selector that takes a parameter! cancel: this means that another parameter will be accepted.

change your method to

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

or

-(IBAction)cancel:(UIButton *)btnSender{
/// Do what you want
}
+1
source

you need to change the cancellation method signature to

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

when you added an action to your cancelButton (during initialization), you specified the select “cancel:” selector, which means that it will be called a method that has one parameter (sender button)

0
source

Source: https://habr.com/ru/post/1794952/


All Articles