UIButton.titleLabel is not as useful as it looks
I have been doing some iPhone development lately . Nothing too amazing, just some test apps to get a feel for the system. Now, some people will tell you that Cocoa Touch is an API sent from God and frankly it is pretty good (especially given what passes for UI on other embedded devices), but that doesn't mean it doesn't have some annoyances.
Here is something that tripped me up for a while. The UIButton class has a property called titleLabel which (obviously) returns the UILabel that is used to display the text of the button. You can use this property to modify the parameters of the label, like so:
m_addButton.titleLabel.font = [UIFont systemFontOfSize: 7]; m_addButton.titleLabel.textColor = [UIColor blackColor]; m_addButton.titleLabel.textAlignment = UITextAlignmentRight;
What you can't do is this:
m_addButton.titleLabel.text = @"Add Stuff";
Although nothing I have found in the documentation says so, the text of the button cannot be set from the titleLabel property. What you have to do is this:
[m_addButton setTitle:@"Add Stuff" forState: UIControlStateNormal];
Setting the title this way works, and has the advantage that you can specify different text for different states:
[m_addButton setTitle:@"Add Stuff" forState: UIControlStateNormal]; [m_addButton setTitle:@"Add Stuff (not now)" forState: UIControlStateDisabled];
This is perhaps not that interesting for text titles, but is an excellent way to control the image the button shows based on whether the button is enabled, highlighted, and/or selected.