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

108

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

8个回答

208

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


谢谢!我的应用程序中有UIButtons,但我没有在代码中提及它们。我使用界面构建器注册了目标-动作机制(为此,我有一些IBAction方法来处理按钮点击),那么我该如何访问这些按钮? - Namratha
1
还想补充一点,如果你想让@property为你声明,可以在XCode 4中按住CTRL键,单击按钮,然后将鼠标拖到视图对应的.h文件中。一个对话框会弹出提示你输入属性名。然后哇啦!你就可以在代码中使用这个属性了! - milesmeow

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

我本来想给这篇文章点赞,但最终决定不这么做。[self.eraseButton setTitle: @"Erase" forState: UIControlStateDisabled]; 这并没有使按钮变暗。如果你已经将 setEnable: 设置为 no,它什么也不会做。 - coolcool1994

21

如果有人在寻找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

1

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")

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 是特定于语音聊天的,但您可以使用自己的本地布尔属性来控制开关。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接