Application terminated when tapping custom button on navigation bar - iphone

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.

Related

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.

IOS 5 block unrecognized selector

I am trying to assign a block to a button object so that each time a button is pressed the block will execute. I have the following defined button subclass that holds the block for each unique button I create.
typedef void (^ButtonPressBlock)();
#interface PhotoButton : UIButton
{
ButtonPressBlock photoButtonPressed;
}
#property (copy) ButtonPressBlock photoButtonPressed;
#end
There is a #synthesize photoButtonPressed in the .m
In a separate UIViewController I #import "PhotoButton.h" and then in that view controller I have a method that creates the buttons. The code looks like this.
PhotoButton* photoButton = [UIButton buttonWithType:UIButtonTypeCustom];
photoButton.frame = CGRectMake(i, j, thumbSize+2, thumbSize+2);
[photoButton setImage:photoThumb forState:UIControlStateNormal];
[photoButton setAdjustsImageWhenHighlighted:NO];
[photoButton setPhotoButtonPressed:^()
{
[self performSegueWithIdentifier:#"photoSegue" sender:aPhoto];
}];
assigning the block to the photoButton fails at execution with a
-[UIButton setPhotoButtonPressed:]: unrecognized selector sent to instance...
Not sure what I am doing wrong as blocks are new to me. I thought this would be a great way to make a thumbnail image responsive to a touch and then segue to the full sized image in another view controller. Now I'm not sure.
This:
PhotoButton* photoButton = [UIButton buttonWithType:UIButtonTypeCustom];
should be:
PhotoButton* photoButton = [PhotoButton buttonWithType:UIButtonTypeCustom];
You were creating an instance of UIButton, not PhotoButton

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

iPhone/iPad - issues with NavigationBar button?

I my application i have added button in NavigationBar like this..
UIBarButtonItem *more=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"search-25by25.png"] style:UIBarButtonItemStylePlain target:self action:#selector(SelectMission:)];
self.navigationItem.rightBarButtonItem = more;
When i am clicking on button application get's shutdown...
If i am doing same thing with normal button it's working fine can any one help me why it's behaving like this?
Try This
UIImage *i=[UIImage
imageNamed:#"search-25by25.png"];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.bounds = CGRectMake( 0, 0, i.size.width, i.size.height );
[myButton setImage:i forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(SelectMission:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *more=[[UIBarButtonItem alloc] initWithCustomView:myButton];
self.navigationItem.rightBarButtonItem
= more;
hope it helps :)
Have you looked in the code for SelectMission:? The code you've posted is only for presenting the button, which from your description appears to be working.
Also if there's anything being dumped into the console (Command-Shift-R)?
Judging by the crash log in your comment, I would say this has nothing to do with the UIBarButtonItem class in particular, and everything to do with your action handler. The crash logs tell the whole story: Your class does not implement a method called SelectMission: that takes one argument. Some caveats about the #selector keyword that you will want to double check:
1) Capitalization. Make sure that the method you implement is SelectMission:. Not selectMission:, selectmission:, Selectmission:, etc.
2) Arguments. The colon indicates that the method SelectMission: takes one argument. If you have implemented it and forgotten the argument it will crash with the exception you posted.
That should help narrow down the issue.