[英]Programmatically change title of UIButton not working in iOS Xcode-Beta 3
嘿,我正在开发一个小应用程序,它根据日期选择器的当前状态更改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方法是一个可能的解决方案,但发现库可能已被更改。 任何建议将不胜感激!
您应该像这样设置标题:
[self.whatButton setTitle:@"What day was it?" forState:UIControlStateNormal];
使用以下代码将按钮的标题设置为不同的状态,就像正常一样
[self.yourButton setTitle:@"Default Title" forState:UIControlStateNormal];
就好像你已经点击按钮选择状态一样
[self.yourButton setTitle:@"Selected Title" forState:UIControlStateSelected];
就像你再次点击你的按钮取消选择状态一样
[self.yourButton setTitle:@"Selected Title" forState:UIControlStateDisable];
以及许多其他状态,您可以在按钮的每个状态中设置不同的文本
尝试更改日期比较的方式 -
-(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];
}
谢谢你的所有建议。 第一个建议是有点偏离基础,因为它不是我想改变的发送者头衔。 第二个建议是我将来会使用的好主意。 我最终用这个解决了这个谜语:
- (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];
}
}`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.