Why it doesn't recognise my selector? - iphone

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:)

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)

How to assign a different selector (canceling previous selector method) for a UIButton after execution of some codes

I have a button name "button1"
i have given a selector for it as below
[button1 addTarget:self action:#selector(buttonPressedMethod:) forControlEvents:UIControlEventTouchUpInside];
now after i have done execution of certain codes. i want this method "buttonPressedMethod:" to be no longer involved in the play.
i want to assign a different selector method. say
[button1 addTarget:self action:#selector(dummymethod:) forControlEvents:UIControlEventTouchUpInside];
What i am getting now is.
Both the methods are called. i want only one method to be called at a time.
Please give some suggestions to cancel a selector method.
remove your first target before adding the new one:
[button1 removeTarget:self action:#selector(buttonPressedMethod:) forControlEvents:UIControlEventTouchUpInside];
Just try it simple. Create a method and addtarget to the button1 then there do the conditional checkup to call another method like
-(void)button1Clicked:(id)sender{
if(check_your_Condition==true) { [self previousMethod];}
else { [self laterMethod];}
}
have a variable to track current action of the button.
SEL currentAction;
// assign action to button using this
currentAction = #selector (newAction:);
[button1 addTarget: self action: currentAction forControlEvents: UIControlEventTouchUpInside];
// remove previous action action before assigning new one
if (currentAction)
{
[button1 removeTarget: self action: currentAction forControlEvents: UIControlEventTouchUpInside];
}
// and then reassign action like explained above
Maintain one boolean flag . When you want call first selector :
if(flag==True)
{
[button1 addTarget:self action:#selector(buttonPressedMethod:) forControlEvents:UIControlEventTouchUpInside];
}
After that make flag false where you want.
if(flag==false)
{
[button1 addTarget:self action:#selector(dummymethod:) forControlEvents:UIControlEventTouchUpInside];
}

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.

Unrecognized Selector Sent To Instance, iPhone Error

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.