在iOS中如何更改按钮的文本并禁用一个按钮?
嘿,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
参数中获得该按钮。如果这还不够(因为您需要在动作之外的其他位置访问按钮),请为按钮声明一个outlet:
@property(retain) IBOutlet UIButton *someButton;
可以在IB中将按钮绑定到控制器,NIB加载代码将在加载接口时设置属性值。
[myButton setTitle: @"myTitle" forState: UIControlStateNormal];
使用UIControlStateNormal
来设置您的标题。
UIButton提供了几种状态,您可以查看:
[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];
button.setTitle("Title", for: .normal)
你可以通过以下方式禁用该按钮:
button.isEnabled = false
.normal
与UIControlState.normal
相同,因为类型可以推断出来。
SWIFT 4 带扩展功能
set:
// 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 是特定于语音聊天的,但您可以使用自己的本地布尔属性来控制开关。