Answers:
嘿Namratha,如果您要更改UIButton的文本和启用/禁用状态,可以很容易地完成以下操作;
[myButton setTitle:@"Normal State Title" forState:UIControlStateNormal]; // To set the title
[myButton setEnabled:NO]; // To toggle enabled / disabled
如果您已经在“界面生成器”中创建了按钮并希望通过代码访问它们,则可以利用它们作为IBAction
调用参数传递的事实:
- (IBAction) triggerActionWithSender: (id) sender;
可以将其绑定到按钮,并在sender
触发操作时在参数中获取按钮。如果这还不够(因为您需要在操作之外的其他地方访问按钮),请为按钮声明一个出口:
@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];
如果有人在Swift中寻找解决方案,然后降落在这里,那就是:
myButton.isEnabled = false // disables
myButton.setTitle("myTitle", for: .normal) // sets text
较旧的代码:
myButton.enabled = false // disables
myButton.setTitle("myTitle", forState: UIControlState.Normal) // sets text
要更改按钮标题:
[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
推断类型相同。
如果要更改标题作为对被点击的响应,则可以在视图控制器委托中按钮的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];
}
}
语音聊天当然是特定于语音聊天的,但是您可以使用本地本地布尔属性来控制切换。
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")