【发布时间】:2011-06-24 16:28:29
【问题描述】:
如何在 iOS 中更改按钮的文本并禁用按钮?
【问题讨论】:
如何在 iOS 中更改按钮的文本并禁用按钮?
【问题讨论】:
嘿,纳姆拉塔, 如果您询问更改 UIButton 的文本和启用/禁用状态,可以通过以下方式轻松完成;
[myButton setTitle:@"Normal State Title" forState:UIControlStateNormal]; // To set the title
[myButton setEnabled:NO]; // To toggle enabled / disabled
如果您在 Interface Builder 中创建了按钮并希望在代码中访问它们,则可以利用它们作为参数传递给 IBAction 调用的事实:
- (IBAction) triggerActionWithSender: (id) sender;
这可以绑定到按钮,当触发操作时,您将在 sender 参数中获得按钮。如果这还不够(因为您需要在动作之外的其他地方访问按钮),请为按钮声明一个出口:
@property(retain) IBOutlet UIButton *someButton;
那么就可以将IB中的按钮绑定到控制器了,NIB加载代码会在加载界面时设置属性值。
【讨论】:
[myButton setTitle: @"myTitle" forState: UIControlStateNormal];
使用UIControlStateNormal 设置您的标题。
UIbuttons提供了几种状态,你可以看看:
[myButton setTitle: @"myTitle" forState: UIControlStateApplication];
[myButton setTitle: @"myTitle" forState: UIControlStateHighlighted];
[myButton setTitle: @"myTitle" forState: UIControlStateReserved];
[myButton setTitle: @"myTitle" forState: UIControlStateSelected];
[myButton setTitle: @"myTitle" forState: UIControlStateDisabled];
【讨论】:
【讨论】:
假设按钮是UIButton:
UIButton *button = …;
[button setEnabled:NO]; // disables
[button setTitle:@"Foo" forState:UIControlStateNormal]; // sets text
请参阅UIButton 的文档。
【讨论】:
更改按钮标题:
[mybtn setTitle:@"My Button" forState:UIControlStateNormal];
[mybtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
禁用:
[mybtn setEnabled:NO];
【讨论】:
在 Swift 3 中,您可以通过以下方式简单地更改按钮的标题:
button.setTitle("Title", for: .normal)
然后您通过以下方式禁用按钮:
button.isEnabled = false
.normal 与 UIControlState.normal 相同,因为类型是推断出来的。
【讨论】:
SWIFT 4 带扩展名
设置:
// set button label for all states
extension UIButton {
public func setAllStatesTitle(_ newTitle: String){
self.setTitle(newTitle, for: .normal)
self.setTitle(newTitle, for: .selected)
self.setTitle(newTitle, for: .disabled)
}
}
并使用:
yourBtn.setAllStatesTitle("btn title")
【讨论】:
如果您想更改标题作为对被点击的响应,您可以在视图控制器委托中按钮的 IBAction 方法中尝试此操作。这会打开和关闭语音聊天。此处不包括设置语音聊天!
- (IBAction)startChat:(id)sender {
UIButton *chatButton = (UIButton*)sender;
if (!voiceChat.active) {
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Voice Chat"
message:@"Voice Chat will become live. Please be careful with feedback if your friend is nearby."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
[voiceChat start];
voiceChat.active = YES;
[chatButton setTitle:@"Stop Chat" forState:UIControlStateNormal];
}
else {
[voiceChat stop];
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Voice Chat"
message:@"Voice Chat is closed"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
voiceChat.active = NO;
[chatButton setTitle:@"Chat" forState:UIControlStateNormal];
}
}
voiceChat 当然是特定于语音聊天的,但是你可以使用你的本地布尔属性来控制开关。
【讨论】: