I can't understand why the selector is not called.
//EDITED
[self.scrollView setContentSize:CGSizeMake(320, 600)];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn addTarget:self
action:#selector(validateTextFields:)
forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:#"Продължи" forState:UIControlStateNormal];
btn.frame = CGRectMake(55, 580, 210, 50);
[self.scrollView addSubview:btn];
-(IBAction)validateTextFields:sender
{
NSLog(#"Called");
}
When I touch the button "Called" is not logged in console. If I change UIControlEventTouchUpInside to UIControlEventTouchDown validateTextFields method is executed.
You are adding a button on UIScrollView, this might be creating problem. Please check this question: iPhone: adding button to scrollview makes button inaccessible to interaction. It may be helpful.
The type for action should be
- (IBAction)validateTextFileds:(id)sender
{
// do your stuff
}
and your #selector parameter in addTarget should look like this: #selector(validateTextFields:)
try this:
[addContact addTarget:self action:#selector(buttonPressed:)forControlEvents:UIControlEventTouchUpInside];
-(IBAction)buttonPressed:(id)sender{
}
Related
in the TVOS Application storyboard mainview I added a subview.
Within the subview there are 3 button created in the very same way (the code here belongs to the subview)
UIButton *b = [UIButton buttonWithType:UIButtonTypeSystem];
b.frame = CGRectMake(left, top, 400, size);
[b setTitle:#"Click Me" forState:UIControlStateNormal];
[b addTarget:self action:#selector(Action_Up) forControlEvents:UIControlEventPrimaryActionTriggered];
[self.view addSubview:b];
...
-(void)Action_Up {
[self ShowData];
}
in the simulator and on the real device I can select the button and press it, but the action is never fired.
Could you help ?
Try this.. it worked for me
button.addTarget(self, action: "someMethodName:", forControlEvents: .PrimaryActionTriggered)
I've a UIButton on cell of UITableView in my VC like this
arrowBtnUp = [UIButton buttonWithType:UIButtonTypeCustom];
arrowBtnUp.frame = CGRectMake(50,(65-19)/2,31, 31);
[arrowBtnUp setBackgroundImage:[UIImage imageNamed:#"arrow_up.png"] forState:UIControlStateNormal];
[arrowBtnUp addTarget:self action:#selector(slideAction) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:arrowBtnUp];
and this my slideAction
-(void) slideAction
{
[arrowBtnUp setBackgroundImage:[UIImage imageNamed:#"arrow_down.png"] forState:UIControlStateNormal];
// also tried with UIControlStateSelected/Highlighted
}
But it didn't work.I found this link but didn't help.
Any suggestion or sample would be appreciated.
Your code should work.Anyways change your code like this and try
-(void) slideAction:(id)sender
{
[sender setBackgroundImage:[UIImage imageNamed:#"arrow_down.png"] forState:UIControlStateNormal];
}
and
[arrowBtnUp addTarget:self action:#selector(slideAction:) forControlEvents:UIControlEventTouchUpInside];
and ensure that you have arrow_down.png in your app bundle
The issue is you are reusing the arrowBtnUp instance of UIButton.
Hence in the slideAction method you won't get the pressed buttons reference.
For getting the reference in the slideAction method you need to pass it as an argument.
So change the code like:
[arrowBtnUp addTarget:self action:#selector(slideAction:) forControlEvents:UIControlEventTouchUpInside];
-(void) slideAction:(UIButton *)myButton
{
[myButton setBackgroundImage:[UIImage imageNamed:#"arrow_down.png"] forState:UIControlStateNormal];
}
If you're trying to change the button image after a touch, you should use the UIButton's control states. You can assign a background image for each "state" and let UIButton determine which to show.
Background image is different than the icon on the button. Try this instead:
[_buttonConfirm setImage:[UIImage imageNamed:#"s.png"] forState:UIControlStateNormal];
If you're trying to change the button image after a touch, you should use the UIButton's control states. You can assign a background image for each "state" and let UIButton determine which to show.
[myButton setBackgroundImage:[UIImage imageNamed:#"arrow_up.png"] forState:UIControlStateNormal];
[myButton setBackgroundImage:[UIImage imageNamed:#"arrow_down.png"] forState:UIControlStateSelected];
There is no need to manually switch the background images in your slideAction: target method.
I'm trying to create a button in Cocoa in mac programmatically, but I don't know how to put a action on this, I'm trying like this:
NSRect frame = NSMakeRect(10, 200, 80, 100);
NSButton *btn = [[NSButton alloc]initWithFrame:frame];
[btn setButtonType:NSMomentaryPushInButton];
[btn setBezelStyle:NSRoundedBezelStyle];
[btn setTitle:#"Click me"];
[btn setAction:#selector(hideLabels:)];
[view addSubview:btn];
but the line [btn setAction:#selector(hideLabels:)]; don't work, how can I create a action here?
the method hideLabels is in function, because I used it with another button.
Does your hideLabels method take an argument?
if not, leave off the : from the selector
I am trying to change Action For the UIButton in ios applicatio. I did Following Code
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(aMethodShow:) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];
In particular Section I want to change Action for this Button .So i did this
[button addTarget:self action:#selector(aMethodHide:) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Hide View" forState:UIControlStateNormal];
Unfortunately This code note Working?
I suggest before adding new target, first invoke removeTarget on UIbutton object then add new target with other action.
I think it will help you
[yourButton removeTarget:nil
action:NULL
forControlEvents:UIControlEventAllEvents];
[yourButton addTarget:self
action:#selector(yourAction:)
forControlEvents:UIControlEventTouchUpInside];
You can use same action target instead of using two target. In one target you have to differentiate like below
-(void)btnAction
{
if(target1)
{
// code for target 1
}
else
{
// code for target 2
}
}
Here target1 is BOOL value which value first set to be YES. And change its value NO whenever you want to perform target 2 code.
I hope this will helps You.
I made an app recently and i had the same situation, but i found another way to solve it so i decided to share my solution with people who may be in the same situation.
I'll try to explain what i did with the context of this question:
I added a tag to the button and i associated it with one of the functions that button needs to call (aMethodShow: for example).
button always call the same function (callSpecificFunc: for example). What callSpecificFunc: does is call either function aMethodShow: or aMethodHide according with the current button tag.
In the particular section in which the button needs to call a different function, i only change the tag of button.
Something like this:
NSInteger tagOne = 1000; //tag associated with 'aMethodShow' func
NSInteger tagTwo = 1001; //tag associated with 'aMethodHide' func
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(callSpecificFunc:) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];
...
// in some part of code, if we want to call 'aMethodShow' function,
// we set the button's tag like this
button.tag = tagOne
...
//Now, if we want to call 'aMethodHide', just change the button's tag
button.tag = tagTwo
...
-(void) callSpecificFunc:(UIButton*)sender
{
NSInteger tagOne = 1000;
NSInteger tagTwo = 1001;
if([sender tag] == tagOne){
//do whatever 'aMethodShow' does
}else {
//do whatever 'aMethodHide' does
}
}
Of course it could be applied for more than 2 functions :)
I initialised my UIButton-deriver liek this:
Button * it = [[Button alloc] initWithFrame:CGRectMake(x, y, image.size.width, image.size.height)];
Then, I do the next:
[(UIButton *)self addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:self];
The first line causes "Does not recognize selector" error.
Selector buttonClicked: looks like this:
-(IBAction) buttonClicked:(id)sender {
if (action) action();
else NSLog(#"Clicked.\n");
}
What am I doing wrong?
Add the action with:
[it addTarget:self
action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
and then add the button with
[view addSubview:it];
And don't create UIButtons with init... use the class method + buttonWithType:.
Be careful to to subclass UIButton, I am not sure if you really want this. Have a look at create uibutton subclass.
You create buttons with +(id)buttonWithType:(UIButtonType)buttonType