views:

227

answers:

1

hi dear friends,

I encountered a weird problem. when I try to add a UIButton in my customized ui view. the button title cannot display. only draw the outside roundrect. regardless invoke setTitle:forState: or button.backgroundColor=[UIColor redColor]; it also doesn't work. but for backgroundColor, at the rectangle angle corner, the bgcolor seems take a little bit filling up.

I initialized this button in "initWithNibName" method of custom view controller. is it ok? even I set this button reference to view controller's view property or just added it into other view. this issue also happens.

+2  A: 

You should set your button values in -viewDidLoad. If you try to do this in -initWithNibNamed:bundle:, the outlets won't be set up yet, and your button value will be nil.

Ben Gottlieb
Thank you Ben, I see, I will try this way later. But I still confused, when I run the simulator, The console doesn't print any crash log. application runs looks fine. I don't know why.
Mikael Guan
Mikael, what you're doing won't actually cause an error: sending messages (like `-setBackgroundColor:`, which is what changing the `backgroundColor` property does) to a nil object just doesn't do anything. In Objective-C parlance, you're “messaging nil”, which the runtime can handle just fine.
Noah Witherspoon
Thanks a lot Noah, I got it.
Mikael Guan