Unrecognized Selector Sent To Instance, iPhone Error - iphone

I get the error
"012-02-10 13:54:52.570 HelloWorld[14275:10103]
-[HelloWorldViewController buttonPressed]: unrecognized selector sent to instance 0x6cc0c50 2012-02-10 13:54:52.572 HelloWorld[14275:10103]
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[HelloWorldViewController
buttonPressed]: unrecognized selector sent to instance 0x6cc0c50'".
This is the offending text:
-(void)buttonPressed:(id)sender {
UIButton *button = (UIButton*)sender;
NSString *text = [button titleForState:UIControlStateNormal];
NSLog(#"%#",text);
}
I know this because if I change the code to this:
-(void)buttonPressed {
NSLog(#"Button Pressed");
}
It then works correctly.
However I need the text from the component that sent the message. The components are not drag and dropped with IB. They are allocated, initialized and placed in the loadView method. To each of my buttons I have added buttonPressed as the action listener.

The error unrecognized-selector could be due to a missing :.
[yourbutton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
instead of
[yourbutton addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
In the first case you call -(void)buttonPressed:(id)sender. In the second instead you call -(void)buttonPressed.
But if you provide more code for your UIButton, it could be simpler to understand what is going on.

In the first (not working) case you have -(void)buttonPressed:(id)sender and in the second (working) you have -(void)buttonPressed. Obviously your button calls the function without argument.

that could be the case when you have added the #selector(buttonPressed:) for button touch event, you have forgotten to put the : with method name.
you can check for the same.

Related

Application terminated when tapping custom button on navigation bar

I made an custom button for a navigation bar, but when I tap it, it terminates
-(void)viewDidLoad
{
UIImage *backButtonImage = [UIImage imageNamed:#"button.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:backButtonImage forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
[backButton addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBackBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = customBackBarItem;
}
-(void)goBackOne
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
the output is
2013-07-28 15:00:37.932 Habit Pal[1562:c07] -[SleepModeViewController back]: unrecognized selector sent to instance 0x9167300
2013-07-28 15:00:37.932 Habit Pal[1562:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SleepModeViewController back]: unrecognized selector sent to instance 0x9167300'
*** First throw call stack:
(0x1c93012 0x10d0e7e 0x1d1e4bd 0x1c82bbc 0x1c8294e 0x10e4705 0x182c0 0x18258 0xd9021 0xd957f 0xd86e8 0x47cef 0x47f02 0x25d4a 0x17698 0x1beedf9 0x1beead0 0x1c08bf5 0x1c08962 0x1c39bb6 0x1c38f44 0x1c38e1b 0x1bed7e3 0x1bed668 0x14ffc 0x213d 0x2065)
libc++abi.dylib: terminate called throwing an exception
(lldb)
You button is trying use the selector back on your SleepModeViewController, but you've actually named the method -goBackOne. You fix it, either rename the -goBackOne method to -back, or change the name of the selector to goBackOne. For example:
// The selector must actually match a method name on the target
[backButton addTarget:self action:#selector(goBackOne) forControlEvents:UIControlEventTouchUpInside];
It's important that the selector name and method names match. The error shows that your problem is that the selector named -back doesn't exist. When your app terminates with these errors, you should check that all your #selector() statements match actual method names.

unrecognized selector while calling using addTarget

I am using addTarget on custom cell button like:
[cell.btnGet addTarget:self action:#selector(getLocal:cell:) forControlEvents:UIControlEventTouchUpInside];
It should be call this method:
-(void)getLocal:(id)sender withcell:(GroupListCell *)cell
{
// code for implement
}
but when I am click the button it is throwing error like:
2013-02-27 14:28:56.533 iOSPlayer[704:11303] -[GroupView getLocal:cell:]: unrecognized selector sent to instance 0x72e1290
2013-02-27 14:28:56.534 iOSPlayer[704:11303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[GroupView getLocal:cell:]: unrecognized selector sent to instance 0x72e1290'
What is the issue here?
try this:
[cell.btnGet addTarget:self action:#selector(getLocal:withcell:) forControlEvents:UIControlEventTouchUpInside];
That action method is called:
getLocal:withcell:
and not:
getLocal:cell:
It also doesn't match any of the acceptable action method signatures, which are:
- (IBAction)doSomething;
- (IBAction)doSomething:(id)sender;
- (IBAction)action:(id)sender forEvent:(UIEvent *)event;
(see this Apple guide)

iOS memory management error - message sent to deallocated instance

I created many buttons before, but for some reason I'm having trouble creating a simple button.
In my viewDidLoad method I created a very basic button:
_button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
_button.frame = CGRectMake(0, 0, 100, 25);
[_button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_button];
- (void)buttonClicked:(id)sender
{
NSLog(#"%#", sender);
NSLog(#"Download issue");
}
But for some reason when I click on it I'm just getting an error
* -[DownloadButtonViewController performSelector:withObject:withObject:]: message sent to deallocated instance 0x6ac2af0
I have no idea what's going wrong as the code is exactly the same as every button I created before ... (probably just having a bad day ...)
Your view controller itself is being deallocated. Maybe you're using ARC and you don't have a strong reference to your view controller, so it's deallocated immediately after creation.
I have just Copy pasted your code and it's working fine here,it might be possible you are realeasing it some where by mistake because that object is Autoreleased it self and H2CO3 as said is also true.

Programmatically adding switch to scrollview throws exception

I am using storyboards and have dragged a scrollview window onto a view. In my code I am programmatically creating a switch object that is somehow not being initialized correctly. The switch appears on the view correctly but whenever I click the switch, an exception is thrown saying
"unrecognized selector sent to instance 0x6a786f0'"
I have also attempted to edit the On/Off text to Yes/No and accessing the switch also throws the same exception. Clearly I have missed something in creating my switch and setting the correct delegates or whatever.
My code to create the switch is..
UISwitch *switchControl = [[UISwitch alloc] initWithFrame:CGRectMake(x, y, 60, 20)];
[switchControl addTarget:inputsView action:#selector(actionSwitch:) forControlEvents:UIControlEventTouchUpInside];
[switchControl setBackgroundColor:[UIColor clearColor]];
//[(UILabel *)[[[[[[switchControl subviews] lastObject] subviews]
// objectAtIndex:1] subviews] objectAtIndex:0] setText:#"Yes"];
//[(UILabel *)[[[[[[switchControl subviews] lastObject] subviews]
// objectAtIndex:1] subviews] objectAtIndex:1] setText:#"No"];
[inputsView addSubview:switchControl];
inputsView is the name of my UIScrollView that I created in my .h file.
I should note, when the exception is called on clicking the switch, in the error the 'reason' is reason: '-[UIScrollView actionSwitch:]. When the error is called by trying to adjust the text, the 'reason' is reason: '-[UIImageView setText:]
Any help on what I am missing would be great.
Thanks
The exception is correct, UIScrolView does not have a method actionSwitch:. The target parameter in addTarget: is the object you want to receive the selector: argument.
If your posted code is in the class that has the actionSwitch: method then you would use self as the target, like so:
[switchControl addTarget:self action:#selector(actionSwitch:) forControlEvents:UIControlEventTouchUpInside];
And as a side note. For a UISwitch you generally want your method called for UIControlEventValueChanged, that way if the user just touches the switch but doesn't "switch" it your method won't be called.
Edit in response to: "I just tried changing to 'self' for the UISwitch and the error still occurs. I haven't created an actionSwitch method."
Yes, your application would still crash because whatever you pass in as the target must implement the selector/method passed in as the selector.
The view controller is the ideal place to implement this method. A very standard implementation of this event target would look like:
-(void)actionSwitch:(UISwitch *)theSwitch{
if (theSwitch.isOn){
// Switch was switched on respond accordingly
}
else {
// Switch was switched off respond accordingly
}
}

Why it doesn't recognise my selector?

I want to change the action of an UIButton and it gives me an exception when touching the button.
[grabRedeem removeTarget:self action:#selector(grabbOffer:) forControlEvents:UIControlEventTouchUpInside];
[grabRedeem addTarget:self action:#selector(redeemOffer:) forControlEvents:UIControlEventTouchUpInside];
grabbOffer and redeemOffer are IBActions that are implemented with no parameters.
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DetailViewController grabbOffer:]: unrecognized selector sent to instance 0x1e8bf0'
What could be the reason?
Have you tried to remove the colon:
[grabRedeem removeTarget:self action:#selector(grabbOffer) forControlEvents:UIControlEventTouchUpInside];
[grabRedeem addTarget:self action:#selector(redeemOffer) forControlEvents:UIControlEventTouchUpInside];
you use the semicolon when you want to use a parameter:
-(void)grabbOffer:(id)sender;
If you function implemented with no parameters use: #selector(grabbOffer) instead #selector(grabbOffer:)