How can I add multiple action in a single UIButton? Life for example,
[btn addTarget:self action:#selector(method1) forControlEvents:UIControlEventTouchUpInside];
[btn addTarget:self action:#selector(method2) forControlEvents:UIControlEventTouchDown];
Thanks
The code you have pasted should work:
[btn addTarget:self action:#selector(method1) forControlEvents:UIControlEventTouchUpInside];
[btn addTarget:self action:#selector(method2) forControlEvents:UIControlEventTouchDown];
I do this all the time. Usually for touchDown and touchUp. The fact that method2 isn't getting called is a bug. Do you have an NSLog() at the beginning of method2?
[btn addTarget:self action:#selector(method1and2) forControlEvents:UIControlEventTouchUpInside];
…
- (void)method1and2 {
[self method1];
[self method2];
}
Related
I want to make an app for iphone. It is a Nonogram something similar to this: http://www.puzzle-nonograms.com/ How can I make the UIButtons changes their color from white to black and reverse?
.h file
UIButton *button;
BOOL clicked;
.m file
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
clicked=YES;
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(changeBtnColor)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#" 1 " forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[self.view addSubview:button];
}
-(void)changeBtnColor{
if (clicked) {
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
clicked=NO;
}
else{
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
clicked=YES;
}
}
I hope it will help. Best of luck..
No need to ivar a boolean, buttons have selected, highlighted and normal states built into them.
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(0, 0, 320, 50)];
[button addTarget:self action:#selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Normal State" forState:UIControlStateNormal];
[button setTitle:#"Selected State" forState:UIControlStateSelected];
[button setBackgroundImage:[UIImage imageNamed:#"normal_image"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"selected_image"] forState:UIControlStateSelected];
[self.view addSubview:button];
}
-(void)buttonPress:(UIButton *)button{
button.selected = !button.selected;
//do your other code here for button selection
}
Otherwise set up the button states inside interface builder and just have the buttonPress selector handle the state change.
The UIButton is set as following code:
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 27, 27)];
[btn setBackgroundImage:[UIImage imageNamed:#"imgUp.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"imgDown.png"] forState:UIControlStateHighlighted];
[btn addTarget:self action:#selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
If I touch the btw quickly, the imgDown.png will not appear but the action btnPressed: is fired. How could it be fixed? Any help is appreciated:)
Add this line in your code:
[btn setBackgroundImage:[UIImage imageNamed:#"imgDown.png"] forState:UIControlStateSelected];
You should call the setHiglighted: method for the button (btn) inside of btnPressed: function.
The following code can solve the problem.
-(void)btnPressedDelay{
//original code in btnPressed: method
}
- (void) btnPressed:(id)sender
{
[self performSelector:#selector(btnPressedDelay) withObject:nil afterDelay:0];
// 0 did the magic here
}
its working fine i will tested it pls check your images and try it :
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 27, 27)];
[button setBackgroundImage:[UIImage imageNamed:#"rainy.jpg"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"Icon.png"] forState:UIControlStateHighlighted];
[button addTarget:self action:#selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view button];
I am new to iPhone,
I want to implement radio button in my app, there are three buttons in my app, button should behave like radio button.
Here is my code snippet,
UIButton *Btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn1.frame=CGRectMake(10, 190, 20, 20);
[Btn1 setImage:[UIImage imageNamed:#"radio-off.png"] forState:UIControlStateNormal];
[Btn1 setImage:[UIImage imageNamed:#"radio-on.png"] forState:UIControlStateSelected];
[Btn1 addTarget:self action:#selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn1];
UIButton *Btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn2.frame=CGRectMake(10, 240, 20, 20);
[Btn2 setImage:[UIImage imageNamed:#"radio-off.png"] forState:UIControlStateNormal];
[Btn2 setImage:[UIImage imageNamed:#"radio-on.png"] forState:UIControlStateSelected];
[Btn2 addTarget:self action:#selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn2];
UIButton *Btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn3.frame=CGRectMake(10, 290, 20, 20);
[Btn3 setImage:[UIImage imageNamed:#"radio-off.png"] forState:UIControlStateNormal];
[Btn3 setImage:[UIImage imageNamed:#"radio-on.png"] forState:UIControlStateSelected];
[Btn3 addTarget:self action:#selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn3];
- (IBAction)RadioButton:(UIButton *)button{
for (UIButton *Radiobutton in [self.view subviews]) {
if ([Radiobutton isKindOfClass:[UIButton class]] && ![Radiobutton isEqual:button]) {
[Radiobutton setSelected:NO];
}
}
if (!button.selected) {
button.selected = !button.selected;
}
}
Any help will be appreciated.
You can use following code.
//Add all the buttons to class level NSMutable array
UIButton *Btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn1.frame=CGRectMake(10, 190, 20, 20);
[Btn1 setImage:[UIImage imageNamed:#"radio-off.png"] forState:UIControlStateNormal];
[Btn1 addTarget:self action:#selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn1];
[self.buttonsArray addObject:Btn1];
UIButton *Btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn2.frame=CGRectMake(10, 240, 20, 20);
[Btn2 setImage:[UIImage imageNamed:#"radio-off.png"] forState:UIControlStateNormal];
[Btn2 addTarget:self action:#selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn2];
[self.buttonsArray addObject:Btn2];
UIButton *Btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn3.frame=CGRectMake(10, 290, 20, 20);
[Btn3 setImage:[UIImage imageNamed:#"radio-off.png"] forState:UIControlStateNormal];
[Btn3 addTarget:self action:#selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn3];
[self.buttonsArray addObject:Btn3];
- (IBAction)RadioButton:(id)sender{
[self resetAllButtons];
UIButton* button=(UIButton*) sender;
[button setImage:[UIImage imageNamed:#"radio-on.png"] forState:UIControlStateNormal];
}
-(void) resetAllButtons{
for(int i=0;i<[self.buttonsArray count];i++){
UIButton* button=[self.buttonsArray objectAtIndex:i];
[button setImage:[UIImage imageNamed:#"radio-off.png"] forState:UIControlStateNormal];
}
}
You are checking all views in self.view.subviews but the buttons are actually added to the UIScrollView (I guess) called scrollVw.
Change the for loop to
for (UIButton *Radiobutton in [self.scrollVw subviews]) {
if the scrollVw is added as a property in the .h file.
Any opposition to using a UISegmentedControl? It works like a radio button, in that it can only have one option selected at a time. If you are deadset on the appearance of radio buttons I would subclass it to change the appearance.
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UISegmentedControl_Class/Reference/UISegmentedControl.html
Alternatively you could simply use the iOS provided Control for something like that:
UISegmentedControl
It can easily handle three segments, is quite flexible and can behave like a radio button where you can select only one segment or multiple.
So I am trying to create a custom UIBarButtonItem, but I can't for the life of me figure out why the title won't show. Maybe someone else can spot it :\
Here is the code I use to create the bar button:
+ (UIBarButtonItem *)barButtonItemWithTitle:(NSString *)title normal:(UIImage *)normal highlighted:(UIImage *)highlight selected:(UIImage *)selected target:(id)target action:(SEL)action
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(0.0f, 0.0f, normal.size.width, normal.size.height)];
[button setTitle:title forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
[button setImage:normal forState:UIControlStateNormal];
[button setImage:selected forState:UIControlStateHighlighted];
[button setImage:selected forState:UIControlStateSelected];
return [[UIBarButtonItem alloc] initWithCustomView:button];
}
self.navigationItem.leftBarButtonItem = [UIBarButtonItem barButtonItemWithTitle:#"Cancel"
normal:[UIImage imageNamed:#"back_button_active.png"]
highlighted:[UIImage imageNamed:#"back_button_highlight.png"]
selected:[UIImage imageNamed:#"back_button_selected.png"]
target:self
action:#selector(popViewController)];
Any help would be appreciated!
P.S. This is in iOS 5 using ARC.
title and image are mutually exclusive properties in UIButton. If you want to have title with custom background then you should use backgroundImage property:
...
[button setBackgroundImage:normal forState:UIControlStateNormal];
[button setBackgroundImage:selected forState:UIControlStateHighlighted];
[button setBackgroundImage:selected forState:UIControlStateSelected];
...
I'm using 10 buttons in my interface and need, from time to time, to change the button's selector.
Am I required to use:
-(void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
before I change the selector or can I just use:
-(void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
I'm concerned that if I change the selector using the addTarget: method sans the removeTarget: method that I'll essentially "stack up" selectors for my UIButton to fire when it is pressed.
Yes you should always remove the previously add target before assigning the new target to the button. Like this---
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(50, 50, 200, 50)];
[btn setTag:101];
[btn addTarget:self action:#selector(method1) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
btn = (UIButton *)[self.view viewWithTag:101];
[btn removeTarget:self action:#selector(method1) forControlEvents:UIControlEventTouchUpInside];
[btn addTarget:self action:#selector(method2) forControlEvents:UIControlEventTouchUpInside];
now if you do this
btn = (UIButton *)[self.view viewWithTag:101];
[btn addTarget:self action:#selector(method2) forControlEvents:UIControlEventTouchUpInside];
then both the methods method1 and method2 will be called.
Hope this helps.
Yes, you will need to remove the old target/action or both the old and new actions will be performed.