How to change UIbutton title once a UIbutton is pressed? - ios5

I am having trouble changing the title of UIButton btn2 once btn1 is pressed. When I use _definition settitle:#"Show Word" forState: UIControlStateNormal it changes the original btn1.
Here is the code for your review,
- (IBAction)mynextPressed:(UIButton *)sender {
//Display the string in the label field
[self.WordDisplay setText:vocabulary];
//reset center button with "show word"
[_definitionPressed setTitle:#"Show Word" forState:UIControlStateNormal];
NSLog(#"displaying word: %#", _definitionPressed.titleLabel);
}

Try something along the lines of:
[btn2 setTitle:#"Show Word"]

u can try this way:
[_definitionPressed setTitle:#"Normal State" forState:UIControlStateNormal];
[_definitionPressed setTitle:#"Selected State" forState:UIControlStateHighlighted];

Related

UIButton : Background Image setting Issue when button state is highlighted?

I have an UIButton, I need to change button's image when button is tapped. Button's background images are set in ViewDidLoad method. Pls refer images attached on this thread.
When I tap on button first time, button property will be changed to selected and "arrow_right.png" image will be set to our Button.
2.On second tap, I observed that when button state is highlighted, But it's background image is not setting. You can see some blur effect instead.
My concern is I saw while toggling UIButton, image is not getting set for highlighted state. when button's state changing from Selected to Normal.
Is it a bug or my mistake?
Thank you.
- (void)viewDidLoad
{
[super viewDidLoad];
[testButton setBackgroundImage:[UIImage imageNamed:#"btn_normal.png"] forState:UIControlStateNormal];
[testButton setBackgroundImage:[UIImage imageNamed:#"btn_pressed.png"] forState:UIControlStateSelected];
[testButton setBackgroundImage:[UIImage imageNamed:#"btn_pressed.png"] forState:UIControlStateHighlighted];
}
- (IBAction)buttonPressed:(id)sender
{
UIButton *button = (UIButton*)sender;
button.selected = !button.selected;
if (button.selected)
{
[button setImage:[UIImage imageNamed:#"arrow_right.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"arrow_right.png"] forState:UIControlStateHighlighted];
[button setImage:[UIImage imageNamed:#"arrow_right.png"] forState:UIControlStateSelected];
}
else
{
[button setImage:[UIImage imageNamed:#"arrow_left.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"arrow_left.png"] forState:UIControlStateHighlighted];
[button setImage:[UIImage imageNamed:#"arrow_left.png"] forState:UIControlStateSelected];
}
}
try this
- (IBAction)buttonPressed:(id)sender
{
UIButton *button = (UIButton*)sender;
if(button.selected) {
[button setSelected:NO]
} else {
[button setSelected:YES]
}
}
i diddn't understand why you need to set image again, all you need to do is set its selected property , button will behave as you expected

How to identify which button is clicked in Objective-C?

I am a new iPad developer.
I have created UIButton programmatically, in which I want to identify user has clicked on which button and according to that I want to do some action.
How should I identify this?
Here is my code snippet:
for(int i=0;i<[_array count];i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.tag=count;
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
button.backgroundColor=[UIColor redColor];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(xpos, 270.0, 200.0, 150.0);
[self.view addSubview:button];
}
This is what I'm doing now: I thought I will assign count to each button and I will pass this count to button click method. Is this a good idea? Is there any other way possible?
On button click I'm calling aMethod:
-(void)aMethod:(id)sender{
NSLog(#"btn clicked");
}
Any help will be appreciated!
-(void)aMethod:(UIButton*)sender{
NSLog(#"btn clicked %d", sender.tag);
}

Can't change background image of custom uiButton for selected state

With my code below, it seems that button is in normal state all the time (always green) eventhough i click on him.
UIButton *gumb = [UIButton buttonWithType:UIButtonTypeCustom];
gumb.frame = CGRectMake(4, 40, 104, 37);
gumb.tag=0;
[gumb setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[gumb setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
[gumb setBackgroundImage:[UIImage imageNamed:#"greenButton.png"] forState:UIControlStateNormal];
[gumb setBackgroundImage:[UIImage imageNamed:#"whiteButton.png"] forState: UIControlStateSelected];
If i set image for normal state to whiteButton and for seleted to greenButton then the button is always white and never green.
[gumb setBackgroundImage:[UIImage imageNamed:#"whiteButton.png"] forState: UIControlStateHighlighted]
use this one instead of selected image
I not sure if Selected state is only used for tab-buttons, 'cause I've tried like DixieFlatline did and sometimes did make the button image change. But method of tt.Kilew is definitely worth trying. Just try:
[gumb setBackgroundImage:[UIImage imageNamed:#"whiteButton.png"] forState: UIControlUIControlStateHighlighted]; and the button image will be changed on click.
Do you set the state of the button as selected in the button click action
- (IBAction)buttonClicked:(id)sender;
{
UIButton *button = (UIButton *)sender;
if(button.tag == 0)// in your case
{
button.selected = YES;
}
// Do something
}
Hope this helps
Code for setting selected state image:
[_button setImage:[UIImage imageNamed:#"whiteButton.png"] forState: UIControlStateHighlighted];
Code for setting Normal state image:
[_button setImage:[UIImage imageNamed:#"whiteButton.png"] forState: UIControlStateNormal];

how can i change button background image when it is clicked in iphone?

how can i change background image of UIButton when it is clicked and it should remain as it is until another button is clicked.
[myButton setImage:[UIImage imageNamed:#"btn_normal.png"] forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:#"btn_highlighted.png"] forState:UIControlStateHighlighted];
[myButton setImage:[UIImage imageNamed:#"btn_highlighted.png"] forState:UIControlStateSelected];
myButton.showsTouchWhenHighlighted = YES;
Fourth line myButton.showsTouchWhenHighlighted = YES; will do the trick. Please try.
Working Code :
// 1) Give tag to all needed button
// 2) check which button is selected by "sender tag"
// 3) set all other buttons to not selected
-(IBAction) buttonPressed:(id)sender
{
if ([sender isSelected])
{
[sender setImage:unselectedImage forState:UIControlStateNormal];
[sender setSelected:NO];
}
else
{
[sender setImage:selectedImage forState:UIControlStateSelected];
[sender setSelected:YES];
}
}
You will need to set the image every time user taps any button on your view.
So when user taps your button, set its image to something that you want(lets assume thats its darker) and the rest of the buttons to normal image(lets assume lighter). Then when user taps other button, repeat the process.
[yourButton setImage:[UIImage imageNamed:youImage] forState:UIControlStateNormal];
Hope this helps.
you need to review UIButtonClass reference to get solutions.
Have you considered using a UISegmentedControl? This supports the function of remaining pressed until another one is pressed. Just check out the docs to find the methods for setting the image and programatically setting the selected segment.

how to add a ui button that calls a number from an array and displays the phone number as the button title

anotherViewController.myLabel = [NSString stringWithFormat:#"Telephone %#",
anotherViewController.title];
this is the declaration for a previus label however im not sure how to do this for a button...
the data array is located made in this format
phoneNumbers = [[NSArray alloc] initWithObjects:#"6940313388", #"4045434218", nil ];
self.phoneNumbers =phoneNumbers;
and then the labels are simply added by
theLabel.text = myLabel;
where mylabel is an NSString
please help ive been trying to solve this for a very long time ...
this is the code i use to make buttons. then you add the button to a view !
edit: you would change the setTitle: part of this to whatever string you wanted the button to display as its title.
//create the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//sets its size
[button setFrame:CGRectMake(10, 10, 300, 50)];
//set title, font size and font color
[button setTitle:#"Sign Up" forState:UIControlStateNormal];
[button setFont:[button.font fontWithSize:22]];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"green_btn_bg.png"] forState:UIControlStateNormal];
//set action of the button
[button addTarget:self action:#selector(signUp:)
forControlEvents:UIControlEventTouchUpInside];