I am getting an error message displayed below when my button is pressed. I do not yet have any code inside my "buttonPressed" method although I don't think that has anything to do with it?
Error Message: "terminate called after throwing an instance of 'NSException'
Program received signal: "SIGABRT".
UIButton * myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setImage:[UIImage imageNamed:#"ButtonStandard.png"] forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:#"ButtonSelected.png"] forState:UIControlStateSelected];
[myButton setShowsTouchWhenHighlighted:YES];
myButton.frame = CGRectMake(0.0, 380.0, 320.0, 100.0);
[myButton addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[contentView addSubview:myButton];
any help would be great.
Change the #selector(buttonPressed) to #selector(buttonPressed:) (note the colon at the end) and change the method itself to:
-(void)buttonPressed:(id)sender {
/* sender will be the UIButton. */
}
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];
In my app am using UIScrollView. I added buttons to UIScrollView.
For each button i assigned image using
- (void)setImage:(UIImage *)image forState:(UIControlState)state;
and actions.
forState i used UIControlStateNormal in viewDidLoad.
And when we click on button image has to change.
For this i used same method but forState value changed to UIControlStateSelected in ViewDidLoad.
But button image is not changing.
any one can help or suggest
You've to use like this
In ViewDidLoad Set images for normal and selected state
-(void)ViewDidLaod
[myButton setImage:[UIImage imageNamed:#"barbutton.png"] forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:#"barbutton1.png"] forState:UIControlStateSelected];
To perform selection image change can be done below code
-(void)buttonclick
[myButton setSelected:YES];
or
[myButton setSelected:NO];
Use this line of code I think this will be helpful for you.
-(void)viewDidLoad
{
UIButton *myButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
[myButton setFrame:CGRectMake(0,3,70,32)];
[myButton setImage:[UIImage imageNamed:#"barbutton.png"] forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(compete) forControlEvents:UIControlEventTouchUpInside];
}
-(void)compete
{
[myButton setImage:[UIImage imageNamed:#"barbutton1.png"] forState:UIControlStateNormal];
}
use
[button setImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"button_highlighed.png"] forState:UIControlStateHighlighted];
i need to place a check box in UItableview.
For that i place a box with out check image is place on button.
my code is,
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
customButton.frame=CGRectMake(100, 10, 100, 100);
[customButton setImage:[UIImage imageNamed:#"unselected.png"] forState:UIControlStateNormal];
[cell.contentView addSubview:customButton];
Now i need to change button image when button clicked.
where can i done this.
and how can i impalement this.
can any one please help me.
Thank u in advance.
//add this line
[customButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
//then
-(void) buttonClicked:(id) sender
{
UIButton * button = (UIButton*) sender;
[button setImage:[UIImage imageNamed:#"selected.png"] forState:UIControlStateNormal];
}
You can write this line of code
[customButton setImage:[UIImage imageNamed:#"unselected2.png"] forState:UIControlEventTouchDown];
before
[cell.contentView addSubview:customButton];
and you ca select different images for different events and states
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.