简体   繁体   中英

Programmatically changing button label in Monotouch

I made a .xib with a button, and referenced it with an outlet to "btnActies"

when i do the following (at any given time) in the back-end C# code

btnActies.TitleLabel.Text = "This is a new label!";

When i build and run the app, the label on the button changes to "This is a new label", but then, if I touch the button, the label reverts to the 'default' text i set in the .xib file.

How do i change the label on a Monotouch UIButton and keep this from happening?

When you want to set some text on a UIButton, you do not do it by altering the text of its TextLabel property. You do it by calling its SetTitle method, passing as the second argument, the button state for which the title will be set at runtime. Chetan Bhalara's answer is correct, here is the C#/MonoTouch equivalent:

btnActies.SetTitle ("title", UIControlState.Normal);

They way you are doing it right now doesn't work, because the label's text is changing internally whenever needed, to the title set in Interface Builder (if you have set it), in this case when you tap the button.

//Improved Formatting

Hello

You can use this code for change the button label for iPhone.

[btnActies.setTitle:@"Title value1" forState:UIButtonStateNormal];

[btnActies.setTitle:@"Title value2" forState:UIButtonStateSelected];

[btnActies.setTitle:@"Title value3" forState:UIButtonStateHighlighted];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM