Set UIButton text

2019-09-10 03:44发布

问题:

It is my text label:

@property (strong, nonatomic) IBOutlet UILabel *textEmail;


 _titleGet = [_dictionaryGeter objectForKey:@"Email"];
    _textEmail.text= _titleGet;

objectForKey:@"Email" from plist

I want to make button. How to make that button title to be equal to the textEmail?

回答1:

There are two ways to set the title of a UIButton.

Option 1 (the wrong way): myButton.titleLabel.text = myString

Option 2 (the right way): [myButton setTitle:myString forState:UIControlStateNormal];

So, your code would be:

_titleGet = [_dictionaryGeter objectForKey:@"Email"];
[_btn setTitle:_titleGet forState:UIControlStateNormal];


回答2:

To change a buttons text is pretty much the same

@property (strong, nonatomic) IBOutlet UIButton *buttonEmail;


_titleGet = [_dictionaryGeter objectForKey:@"Email"];
_textEmail.text= _titleGet;

buttonEmail.text =  _titleGet;


回答3:

Your code is great just a small bug i found: Just change

_textEmail.text= _titleGet;

to

_textEmail.titleLabel.text= _titleGet;


回答4:

Try This

_titleGet = [_dictionaryGeter objectForKey:@"Email"];

 NSString *valueToSave = _titleGet
[[NSUserDefaults standardUserDefaults] setObject:valueToSave forKey:@"keyYourChoice"];
[[NSUserDefaults standardUserDefaults] synchronize];


NSString *xyx = [[NSUserDefaults standardUserDefaults]
stringForKey:@"keyYourChoice"];

 _textEmail.text= xyx;

THANK YOU