changing title of UIButton not working - ios

I have added a button in tableview cell. My problem is this when I change its title on calling method pressHonkBtn:(id)sender it's not changing its title.
How to solve this.

Use your code as this :
Objective-C:
-(IBAction)pressHonkBtn:(id)sender
{
UIButton *tempBtn = (UIButton *)sender;
[tempBtn setTitle:#"YOUR_TITLE" forState:UIControlStateNormal];// YOUR_TITLE is your button title
[tempBtn setTitle:#"YOUR_TITLE" forState:UIControlStateHighlighted];
}
Swift:
someUIButton.setTitle("String To Set", forState: UIControlState.Normal)
Hope it helps you.

Please try to use this one.....It will work fine.
-(IBAction)pressHonkBtn:(id)sender
{
UIButton *btn = (UIButton *)sender;
[btn setTitle:#"title" forState:UIControlStateNormal];
}

Try this:
-(IBAction)pressHonkBtn:(id)sender
{
UIButton *btn = (UIButton *)sender;
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[btn setTitle:#"title" forState:UIControlStateNormal];
[btn setTitle:#"title" forState:UIControlStateHighlighted];
}

Since your are recieving the id as argument you need to type cast it to UIButton .
-(IBAction)pressHonkBtn:(id)sender
{
UIButton *senderBtn = (UIButton *)sender;
[senderBtn setTitle:[NSString stringWithFormat:#"Title"] forState:UIControlStateNormal];
}

Related

Make navigation bar title clickable Objective C iOS 10.2

Is there any way to make the navigation bar title clickable? I don't have any specific class for navigation bar. I control everything from ViewController. If there's a way, I would love to know. I have been searching it for quite a white but couldn't find a suitable answer.
Make button on Navigation bar
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(btnclick:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Title" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, self.view.frame.size.width, 64.0);
self.navigationItem.titleView =button;
-(void)btnclick:(id)sender
{ NSLog(#"button click");
}
You can try like this way
UIView *topView = [[UIView alloc]initWithFrame:CGRectZero];
UIButton * button = [[UIButton alloc]initWithFrame:CGRectZero];
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Title here" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button sizeToFit];
[topView addSubview:button];
[topView sizeToFit];
self.navigationItem.titleView = topView;
-(void)buttonAction:(Id) sender
{
NSLog(#"button clicked");
}

Call methods on UIButton objects in NSMutableArray Objective-C

I have an array called buttons which consists of UIButton objects. I populate it as follows:
for (int i=0; i<self.numButtons; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Title" forState:UIControlStateNormal];
button.frame = CGRectMake(xCoord-40, y, 80, 20);
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[button setTag:i];
[self.buttons addObject:button];
[self.view addSubview:button];
y+= 45;
}
The action for these buttons is buttonAction. This action is supposed to change the color of the selected button. Here is how I have it implemented:
-(void) buttonAction:(id)sender{
int num = (int)[sender tag];
[[self.buttons objectAtIndex:num] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
NSLog(#"%#", [self.buttons objectAtIndex:buttonClicked].titleLabel.text);
}
The result is that there is no color change. Even when I try to NSLog the title of the button it returns (null). So my question is how can I fix this but also how do I call methods on objects inside arrays? Any help would be appreciated. Thanks!
All evidence points to self.buttons being nil. Did you ever initialize it?
Somewhere you need a line like:
self.buttons = [NSMutableArray array];
Of course you could use the sender parameter to your buttonAction: method:
- (void)buttonAction:(UIButton *)button {
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
Note the change in the parameter.
I like to do this. Don't forget to use IBAction. It will help if you decide to use a nib or storyboard.
-(IBAction) buttonAction:(id)sender{
[((UIButton*)sender) setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]
NSLog(#"%#", ((UIButton*)sender).titleLabel.text)
}

Change button image by clicking on the button IOS7

I'm to trying change the button image after tapping on it but its not working.
- (IBAction)click:(id)sender {
UIButton *btn = (UIButton*)sender;
[btn setBackgroundImage:[UIImage imageNamed:#"vv.png"] forState:UIControlStateHighlighted];
}
I have seen the many solutions which given here its not working for me please tell me is there any other way to make it done.
Try UIControlStateNormal
[btn setBackgroundImage:[UIImage imageNamed:#"vv.png"] forState:UIControlStateNormal];
Try this
[btn setBackgroundImage:[UIImage imageNamed:#"vv.png"] forState:UIControlStateNormal];
If you are after something like "toggle button" you can use the following code
- (IBAction)click:(id)sender {
UIButton *btn = (UIButton*)sender;
btn.selected = !btn.selected;
}
previously setting up the image for the selected state in nib/storyboard or programmatically:
- (void)viewDidLoad {
[super viewDidLoad];
[self.btn setBackgroundImage:[UIImage imageNamed:#"vv.png"] forState:UIControlStateSelected];
}
This way if you press the button again it will get back to the original image.
If you want to change image to "vv.png" for good then just use:
[btn setBackgroundImage:[UIImage imageNamed:#"vv.png"] forState:UIControlStateNormal];
use
(IBAction)click:(UIButton *)sender
instead of
(IBAction)click:(id)sender
Try this
- (IBAction)click:(id)sender {
UIButton *btn = (UIButton*)sender;
[btn setImage:[UIImage imageNamed:#"vv.png"] forState:UIControlStateNormal];
}

I want to change the UIButton Highlight Image, but without success

I have a UIbutton in my view
I want to change the UIButton Highlight Image, but without success
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 66, 29)];
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_normal.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_selected.png"] forState:UIControlStateHighlighted];
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_down_selected.png"] forState:UIControlStateSelected];
[button addTarget:self action:#selector(filterAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
- (void)filterAction:(UIButton *)button
{
if(button.isSelected == YES)
{
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_selected.png"] forState:UIControlStateHighlighted];
button.selected = NO;
}
else
{
#warning I want to change the UIButton Highlight Image, but without success
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_down_normal.png"] forState:UIControlStateHighlighted];
button.selected = YES;
}
}
If you want your button to behave as follows,
normal - filter_button_normal
highlighted (on touch press down on button) - filter_button__down_selected
selected (after removing touch from button) - filter_button_selected;
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_normal.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_selected.png"] forState:UIControlStateSelected];
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_down_selected.png"] forState:UIControlStateHighlighted];
so when the user touches it , image changes to highlighted and when he removes his finger over it, it changes to selected
And the target you added is UIControlEventTouchUpInside
[button addTarget:self action:#selector(filterAction:) forControlEvents:UIControlEventTouchUpInside];
so filterAction is called after the user removes his finger from it.
- (void)filterAction:(UIButton *)button
{
// the image will change automatically
if(button.isSelected == YES)
{
button.selected = NO;
}
else
{
button.selected = YES;
}
}

Set permanent button image after tap

i've programmatically created a button with an image for normalState.
But I want to set a new button image when the button is pressed (the new image should be displayed for the rest of the time). I tried something, but it only works during tapping. So the button shows its old image after tapping again.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:#"image1.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"image2.png"] forState:UIControlStateHighlighted];
button.frame = CGRectMake(15.0f, 32.0f, 24.0f, 20.0f);
[cell addSubview:button];
Thanks for your help.
Try to use this it will helps you.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:#"image1.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"image2png"] forState:UIControlStateHighlighted];
button.frame = CGRectMake(15.0f, 32.0f, 24.0f, 20.0f);
// ----------- Add this line in your code -------
[button addTarget:self action:#selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
- (void)btnPressed:(UIButton *)sender
{
[sender setBackgroundImage:[UIImage imageNamed:#"image2.png"] forState:UIControlStateNormal];
}
You have to do the setting in the target of the button. Add the following code:
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
and set the image in that method for UIControlStateNormal:
- (void)buttonTapped:(UIButton *)sender {
[button setBackgroundImage:[UIImage imageNamed:#"image2png"] forState:UIControlStateNormal];
}

Resources