如何更改按钮的文本并禁用iOS中的按钮?
答案 0 :(得分:201)
嘿Namratha, 如果您要求更改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加载代码将在加载接口时设置属性值。
答案 1 :(得分:20)
[myButton setTitle: @"myTitle" forState: UIControlStateNormal];
使用UIControlStateNormal
设置标题。
UI按钮提供了几种状态,您可以查看:
[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];
答案 2 :(得分:17)
如果正在寻找Swift解决方案的人来到这里,那就是:
myButton.isEnabled = false // disables
myButton.setTitle("myTitle", for: .normal) // sets text
旧代码:
myButton.enabled = false // disables
myButton.setTitle("myTitle", forState: UIControlState.Normal) // sets text
答案 3 :(得分:9)
假设按钮是UIButton
:
UIButton *button = …;
[button setEnabled:NO]; // disables
[button setTitle:@"Foo" forState:UIControlStateNormal]; // sets text
请参阅UIButton
的文档。
答案 4 :(得分:3)
更改按钮标题:
[mybtn setTitle:@"My Button" forState:UIControlStateNormal];
[mybtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
禁用:
[mybtn setEnabled:NO];
答案 5 :(得分:3)
在Swift 3中,您只需通过以下方式更改按钮的标题:
button.setTitle("Title", for: .normal)
并通过以下方式禁用按钮:
button.isEnabled = false
.normal
与UIControlState.normal
相同,因为推断了类型。
答案 6 :(得分:0)
如果要将标题更改为对被点击的响应,可以在视图控制器委托中按钮的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当然特定于语音聊天,但您可以使用ow本地布尔属性来控制切换。
答案 7 :(得分:0)
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")