Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 773
Timothy Groote Avatar asked Apr 11 '11 14:04

Timothy Groote


1 Answers

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.

like image 107
Dimitris Tavlikos Avatar answered Oct 27 '22 08:10

Dimitris Tavlikos