I am trying to use addTarget:Action:forControlEvents but I am receiving a runtime exception for an unrecognized selector.
This is being called from a UIView subclass in initWithFrame.
Below is my code:
myButton = [[UIButton alloc] initWithFrame:frame];
[myButton addTarget:self action:&selector(displayList:)
forControlEvents:UIControlEventTouchUpInside];
My method:
-(void)displayList:(id)sender
When I click on the button it receive the following message:
-[MyClass displayList:]: unrecognized selector sent to instance.
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: -[MyClass displayList:]: unrecognized selector sent to instance.
MyClass is a custom control that is a UIView subclass with a UIButton and UILabel. This class is going to be placed on a ViewController of another application.
I'm not sure what I am missing. Any help would be greatly appreciated.
it should read
[myButton addTarget:self action:#selector(displayList:) forControlEvents:UIControlEventTouchUpInside];
# instead of &
[myButton addTarget:self action:&selector(displayList:) forControlEvents:UIControlEventTouchUpInside];
The syntax is #selector(displayList:)
Also, there is a change your button may not fire the event. Try creating it this way:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; //this will return an autoreleased UIButton object, be careful.
button.frame = CGRectMake(...);
...
Related
I have subclass of UIButton:
#interface MyButton : UIButton
- (IBAction) buttonPressed:(id)sender;
#end
#implementation MyButton
- (void)awakeFromNib
{
NSLog(#"awake from nib");
}
- (IBAction) buttonPressed:(id)sender
{
NSLog(#"button pressed");
}
#end
I add a button in my parent xib, set it's class to MyButton, and connected it's action to First Responder's method buttonPressed.
When I start an application and load my parent xib with my button inside, than awakeFromNib from MyButton class is called. But when I press the button, nothing happens. I was expecting that my method buttonPressed from MyButton class will be called.
I supposed, that my button's view is the first responder in responder chain, but apparently I do something wrong.
Could you please suggest something?
If you want that your button always call the method declared add the code above to the awakeFromNib method
[self addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
and later when you added it to a view controller do not assign it a new action, this way it will always call your method.
Depending on what you are doing with the button - there is no real need to make a subclass.
Here is some code how to make a simple button that triggers a method:
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[myButton setFrame:CGRectMake(10, 10, 100, 30)];
[myButton setTitle:#"Title" forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(myMethod:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:myButton];
the add target bit tells the button to trigger the given selector when the user touches up inside the button (taps it). The target is the object that the selector is in.
Hope this helps, you should not need to subclass UIButton unless you have custom layouts or specific custom functionality for it.
Since you want to handle the action from the button code itself, you may have to override this method - (void)sendActionsForControlEvents:(UIControlEvents)controlEvents and handle your event as if it was sent to your button;
So i have a button in a view i know how to access it but i have no idea how to change picture on that button. Here is what i got:
for(UIView *button in [okvir subviews]){
if([button isKindOfClass:[UIButton class]]){
UIImage *picture=[UIImage imageWithData:slika];
[button setImage:picture forState:UIControlStateNormal];
}
}
However that results in a error:
No visible #interface for 'UIView' declares the selector 'setImage:forState:'
I'm guessing that's because button is UIView not UIButton, any idea how to do that?
You need to cast your pointer so that the compiler recognizes it as being a UIButton and not a generic UIView. This is entirely safe since you are checking the runtime type of your pointed object, so you can be sure it is a UIButton.
Use this:
[(UIButton*)button setImage:picture forState:UIControlStateNormal];
I can't believe i'm stumbling on something so simple:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Tap me" forState:UIControlStateNormal];
[button addTarget:self action:#selector(test) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(50, 50, 120, 60);
[self.view addSubview:button];
}
return self;
}
-(void)test {
NSLog(#"Test");
}
It crashes when I press the button, with an unrecognized selector sent to instance error.
Anybody know what I could be doing wrong here?
Edit - error message:
-[__NSCFString test]: unrecognized selector sent to instance 0x29ee30
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString test]: unrecognized selector sent to instance 0x29ee30'
Edit - how it's presented (ARC):
DemoViewController *demoVC = [[DemoViewController alloc] init];
[self.window addSubview:demoVC.view];
[self.window makeKeyAndVisible];
If you are using ARC, then demoVC.view will be realeasd just after the function ends, instead of just initializing like this
DemoViewController *demoVC = [[DemoViewController alloc] init];
Make a strong property around demoVC and initialize it as this
self.demoVC = [[DemoViewController alloc] init];
Your error message indicates (most likely) that your view controller is being deallocated before the button is pushed. (It shows that the message is being sent to an NSString, which is probably occupying the memory that the view controller used to be at.)
The easiest way to hunt this down is to use zombies to confirm this and identify why your object is being deallocated early.
EDIT: Based on your presentation code, you should make, for example, an instance variable to keep a reference to your view controller. You probably also want to move your button initialization code to viewDidLoad to avoid other problems in the future. (Or just hook up the button in a nib!)
when adding any UI Component with code it need to add in viewDidLoad or loadView(if you don`t add xib file) override method.
and also you need to add parameter for sender (this is recommandation)
ex ) - (void)test:(id)sender
and add target like this.... [button addTarget:self action:#selector(test:) forControlEvents:UIControlEventTouchUpInside]
You might calling "test" method for NSString somewhere in your code.
My ViewController.m creates an instance of a myUIView.
In myUIView a UIButton is created.
All seems good, except to capture the button presses I use addTarget, at the ViewController level.
Pressing the button causes a crash, saying: "unrecognized selector sent to instance 0x6c254e0..."
Is this addTarget code wrong? Would appreciate anyone's assistance.
- (void)viewDidLoad{
<UIVIew implementation etc...>
[myUIView.myButton addTarget:self action:#selector(myButtonIsPressed:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)myButtonIsPressed{
NSLog(#"Pressed!");
}
Thanks.
Simply remove : after myButtonIsPressed in the line :
[myUIView.myButton addTarget:self action:#selector(myButtonIsPressed:) forControlEvents:UIControlEventTouchUpInside];
I am getting my UIButton with its tag bay name by using.
UIButton *mybutton=[[UIButton alloc]init];
mybutton=(UIButton*)[cell viewWithTag:5];
and with
UIButton* mybutton=(UIButton*)[cell viewWithTag:5];
This is fine.but when i change it's text(with tag button) by using
[mybutton setTitle:#"hello" forState:UIControlStateNormal];
App Crashes with this error
[UITableViewCell setTitle:forState:]: unrecognized selector sent to instance 0x4b6f540'
* Call stack at first throw:
Any Solution??
Thanks in advance
Use with your contentView
mybutton=(UIButton*)[cell.contentView viewWithTag:5];