【问题标题】:change text of button and disable button in iOS在 iOS 中更改按钮的文本和禁用按钮
【发布时间】:2011-06-24 16:28:29
【问题描述】:

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

【问题讨论】:

    标签: ios button text


    【解决方案1】:

    嘿,纳姆拉塔, 如果您询问更改 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 参数中获得按钮。如果这还不够(因为您需要在动作之外的其他地方访问按钮),请为按钮声明一个出口:

    @property(retain) IBOutlet UIButton *someButton;
    

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

    【讨论】:

    • 谢谢!我的应用程序中有 UIButtons,但我没有在任何地方的代码中提到它们。我已经使用界面构建器来注册目标动作机制(为此,我有一些 IBAction 方法来处理按钮点击)那么我如何访问按钮??
    • 如果你想为你声明@属性,还想补充一点。在 XCode 4 中,按住 CTRL 键,单击按钮,然后将鼠标拖到视图对应的 .h 文件中。将弹出一个对话框,提示您输入属性名称。然后瞧,您将拥有可在代码中使用的属性!
    【解决方案2】:
    [myButton setTitle: @"myTitle" forState: UIControlStateNormal];
    

    使用UIControlStateNormal 设置您的标题。

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

    【讨论】:

    • 我打算为这篇文章 +1,但决定不这样做。 [self.eraseButton setTitle: @"Erase" forState: UIControlStateDisabled];这不会使按钮变暗。如果您已经将 setEnable: 设置为 no,则它什么也不做。
    【解决方案3】:

    如果有人在 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:)'
    【解决方案4】:

    假设按钮是UIButton

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

    请参阅UIButton 的文档。

    【讨论】:

      【解决方案5】:

      更改按钮标题:

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

      禁用:

      [mybtn setEnabled:NO];
      

      【讨论】:

        【解决方案6】:

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

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

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

        button.isEnabled = false
        

        .normalUIControlState.normal 相同,因为类型是推断出来的。

        【讨论】:

        • 如果是动作按钮我们应该使用什么?
        【解决方案7】:

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

        【讨论】:

          【解决方案8】:

          如果您想更改标题作为对被点击的响应,您可以在视图控制器委托中按钮的 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 当然是特定于语音聊天的,但是你可以使用你的本地布尔属性来控制开关。

          【讨论】:

            猜你喜欢
            • 2018-04-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-02-07
            • 1970-01-01
            • 2018-07-17
            • 1970-01-01
            相关资源
            最近更新 更多