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


106

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

Answers:


208

嘿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加载代码将在加载接口时设置属性值。


谢谢!我的应用程序中有UIButtons,但代码中都没有提到它们。我已经使用界面生成器注册了目标动作机制(为此,我有一些IBAction方法来处理按钮单击),所以我该如何访问按钮?
Namratha

1
如果要为您声明@属性,也要添加。在XCode 4中,按住CTRL键,单击按钮,然后将鼠标拖到视图的相应.h文件中。弹出对话框,提示您输入属性名称。瞧,您将拥有该属性供您在代码中使用!
milesmeow,2011年

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

我本来打算为此信息+1,但决定不这样做。[self.eraseButton setTitle:@“ Erase” forState:UIControlStateDisabled]; 这不会使按钮变暗。如果您已经有setEnable:,则它什么都不做。
coolcool1994

18

如果有人在Swift中寻找解决方案,然后降落在这里,那就是:

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

文档:isEnabledsetTitle

较旧的代码:

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

'setTitle(:forState :)'已重命名为'setTitle(:for :)'
Lavi Avigdor

10

假设按钮是UIButton

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

请参阅的文档UIButton


3

要更改按钮标题:

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

禁用:

[mybtn setEnabled:NO];

3

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

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

您可以通过以下方式禁用该按钮:

button.isEnabled = false

.normalUIControlState.normal推断类型相同。


如果它是一个操作按钮,我们应该使用什么?
Karthikeyan Sk Sk,2017年

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

}

语音聊天当然是特定于语音聊天的,但是您可以使用本地本地布尔属性来控制切换。


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")
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.