我正在编写一个应用程序,该应用程序加载带有多个按钮的视图(在.xib中设计)。加载视图时,我以编程方式设置按钮标题。然后我将视图添加为子视图。在一瞬间,我看到程序设置的标题出现了,然后它们变成了.xib.中的原型标题
这是代码:
self.cellPhoneButton.titleLabel.text = person.cellPhone;
self.homePhoneButton.titleLabel.text = person.homePhone;
// Do other stuff with the view
[self.peopleView addSubview:self.personView];
为什么按钮的标题会恢复到默认内容,为什么页面的其他组件(标签、导航栏标题等)不会出现这种情况?
编辑:
我也尝试过这个代码,但没有成功:
[self.cellPhoneButton setTitle:person.cellPhone forState:UIControlStateNormal|UIControlStateSelected];
[self.homePhoneButton setTitle:person.homePhone forState:UIControlStateNormal|UIControlStateSelected];
// Do other stuff with the view
[self.peopleView addSubview:self.personView];
您应该使用[self.homePhoneButton setTitle: forState:];
方法来设置UIButton
的标题。并且您的情况下的状态应该是UIControlStateNormal
,这意味着如果您没有明确指定其他状态,则控制状态将应用于所有状态。如果您将Normal与其他Normal组合,那么Normal将被过度添加。