user549767
user549767

Reputation: 437

how to change the button text programmaticaly in iphone

Im new to iphone development.here i added some list of names in pickerview.the action of pickerview was given to button in view. now i want to show.when click the button pickerview list was displayed i selected one name regarding in pickerview that name was displayed at button in view. I dont no how to change the title in button in iphone.

Can any one plz give me information for my problem.

thank you in advance.

Upvotes: 1

Views: 2102

Answers (6)

aarti Garg
aarti Garg

Reputation: 161

To change the custom button title use this:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(82,203,129,45);

[button setTitle:@"btnTitle" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:button];
[self.view bringSubviewToFront:button];

Upvotes: 2

Ishu
Ishu

Reputation: 12787

declare your button and make property of your button in .h file

UIButton *btnl;

@property(nonatomic,retain) IBOutlet UIButton *btnl;

and make connection from IB.

Now in .m file,

use this delegate method of you picker

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
   btn.text=[yourArrayOfPickerContent objectAtIndex:row];
}

Upvotes: 2

Mayur Birari
Mayur Birari

Reputation: 5835

This way you can code about UIButton and can set the title to it.

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];                               
[btn setFrame:CGRectMake(0.0f, 4.0f, 90.0f, 20.0f)];
[btn setBackgroundColor:[UIColor clearColor]];                              
UIImage *backgroundView = [UIImage imageNamed:@"btn.png"];
[btn setBackgroundImage:backgroundView forState:UIControlStateNormal];
[btn setTitle:@"buttonName" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];          

Upvotes: 1

KingofBliss
KingofBliss

Reputation: 15115

To change button title,

  button.titleLabel.text=@"String";

You should Code in the pickerView delegate

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

Upvotes: 1

Robin
Robin

Reputation: 10011

first you need to create a pointer of type UIButton as IBOutlet in your view controller and than connect that button to the one in IB.

when you want to change the name of title just do

button.titleLabel.text = [NSString stringWithString:yourString];

Upvotes: 0

bamana
bamana

Reputation: 1625

You should be able to change the title of a UIButton by calling setTitle. Just remember to set it for all the states of the IUIButton. Here is an example:

[button setTitle:@"Normal Title" forState:UIControlStateNormal];
[button setTitle:@"Highlighted Title" forState:UIControlStateHighlighted];

Upvotes: 2

Related Questions