嘿我正在开发一个小应用程序,它根据日期选择器的当前状态更改UIButton的文本。它要么显示"那是什么日子?"或"它会是哪一天?"。但是对于xcode 6.3 beta,UIButton SetTitle方法似乎不存在。相反,我尝试将一个字符串分配给titleLabel.txt,但这不会更改按钮文本。
方法如下:
(IBAction) changeButton:(id)sender {
NSDate *currentDate = [self.datePicker date];
NSDate *now = [NSDate date];
if(currentDate<now){
self.whatButton.titleLabel.text = @"What day was it?";
}
else {
self.whatButton.titleLabel.text = @"What day will it be?";
}
我已经看到setTitle方法是一种可能的解决方案,但发现库可能已被更改。任何建议将不胜感激!
答案 0 :(得分:5)
您应该像这样设置标题:
[self.whatButton setTitle:@"What day was it?" forState:UIControlStateNormal];
答案 1 :(得分:4)
使用以下代码将按钮的标题设置为不同的状态,就像正常
一样 [self.yourButton setTitle:@"Default Title" forState:UIControlStateNormal];
好像你已经点击按钮选择状态,它必须是
[self.yourButton setTitle:@"Selected Title" forState:UIControlStateSelected];
好像您再次点击了按钮以取消选择状态,它必须是
[self.yourButton setTitle:@"Selected Title" forState:UIControlStateDisable];
以及许多其他状态,您可以在按钮的每个状态中设置不同的文本
答案 2 :(得分:0)
尝试更改日期比较的方式 -
-(IBAction)changeButton:(id)sender {
NSDate *currentDate = [self.datePicker date];
NSDate *now = [NSDate date];
NSString *titleString = ([currentDate timeIntervalSinceReferenceDate] < [now timeIntervalSinceReferenceDate]) ? @"What day was it?" : @"What day will it be?";
[self.whatButton setTitle:titleString forState:UIControlStateNormal];
}
答案 3 :(得分:0)
谢谢你的所有建议。第一个建议是有点偏离基础,因为它不是我想改变的发送者头衔。第二个建议是我将来会使用的好主意。我最终用这个解决了这个谜语:
- (IBAction) changeButton:(id)sender {
NSDate *currentDate = [self.datePicker date];
NSDate *now = [NSDate date];
UIButton *tempBtn = (UIButton *)self.whatButton;
UIControlState state = tempBtn.state;
if([currentDate compare:now] == NSOrderedAscending){
[tempBtn setTitle:@"What day was it?" forState:state];
}
else {
[tempBtn setTitle:@"What day will it be?" forState:state];
}
}`