更改按钮文本并在 iOS 中禁用按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4954608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:51:10  来源:igfitidea点击:

change text of button and disable button in iOS

iosbuttontext

提问by Namratha

How do you change the text of the button and disable a button in iOS?

如何更改按钮的文本并禁用 iOS 中的按钮?

回答by MGunetileke

Hey Namratha, If you're asking about changing the text and enabled/disabled state of a UIButton, it can be done pretty easily as follows;

嘿 Namratha,如果您要更改 UIButton 的文本和启用/禁用状态,可以很容易地完成,如下所示;

[myButton setTitle:@"Normal State Title" forState:UIControlStateNormal]; // To set the title
[myButton setEnabled:NO]; // To toggle enabled / disabled

If you have created the buttons in the Interface Builder and want to access them in code, you can take advantage of the fact that they are passed in as an argument to the IBActioncalls:

如果您已经在 Interface Builder 中创建了按钮并希望在代码中访问它们,您可以利用它们作为IBAction调用参数传入的事实:

- (IBAction) triggerActionWithSender: (id) sender;

This can be bound to the button and you'll get the button in the senderargument when the action is triggered. If that's not enough (because you need to access the buttons somewhere else than in the actions), declare an outlet for the button:

这可以绑定到按钮,sender当触发动作时,您将在参数中获得按钮。如果这还不够(因为您需要在操作之外的其他地方访问按钮),请为按钮声明一个出口:

@property(retain) IBOutlet UIButton *someButton;

Then it's possible to bind the button in IB to the controller, the NIB loading code will set the property value when loading the interface.

那么就可以将IB中的按钮绑定到控制器上,NIB加载代码会在加载界面时设置属性值。

回答by Namratha

[myButton setTitle: @"myTitle" forState: UIControlStateNormal];

Use UIControlStateNormalto set your title.

使用UIControlStateNormal设置你的标题。

There are couple of states that UIbuttons provide, you can have a look:

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];

回答by mikiqex

If somebody, who is looking for a solution in Swift, landed here, it would be:

如果有人在 Swift 中寻找解决方案,来到这里,它将是:

myButton.isEnabled = false // disables
myButton.setTitle("myTitle", for: .normal) // sets text

Documentation: isEnabled, setTitle.

文档:isEnabledsetTitle

Older code:

旧代码:

myButton.enabled = false // disables
myButton.setTitle("myTitle", forState: UIControlState.Normal) // sets text

回答by zoul

Assuming that the button is a UIButton:

假设按钮是一个UIButton

UIButton *button = …;
[button setEnabled:NO]; // disables
[button setTitle:@"Foo" forState:UIControlStateNormal]; // sets text

See the documentation for UIButton.

请参阅 的文档UIButton

回答by lmiguelvargasf

In Swift 3, you can simply change the title of a button by:

在 Swift 3 中,您可以通过以下方式简单地更改按钮的标题:

button.setTitle("Title", for: .normal)

and you disable the button by:

然后通过以下方式禁用按钮:

button.isEnabled = false

.normalis the same as UIControlState.normalbecause the type is inferred.

.normalUIControlState.normal推断类型相同。

回答by Bhavin Ramani

To Change Button title:

更改按钮标题:

[mybtn setTitle:@"My Button" forState:UIControlStateNormal];
[mybtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

For Disable:

对于禁用:

[mybtn setEnabled:NO];

回答by Tim

If you want to change the title as a response to being tapped you can try this inside the IBAction method of the button in your view controller delegate. This toggles a voice chat on and off. Setting up the voice chat is not covered here!

如果您想更改标题以响应被点击,您可以在视图控制器委托中按钮的 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 is specific to voice chat of course, but you can use your ow local boolean property to control the switch.

voiceChat 当然特定于语音聊天,但您可以使用 ow 本地布尔属性来控制开关。

回答by Oz Shabat

SWIFT 4with extension

带扩展程序的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)
    }
}

and use:

并使用:

yourBtn.setAllStatesTitle("btn title")