change text of button and disable button in iOS

后端 未结 8 873
Happy的楠姐
Happy的楠姐 2020-12-23 12:52

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

相关标签:
8条回答
  • 2020-12-23 13:26

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

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

    and you disable the button by:

    button.isEnabled = false
    

    .normal is the same as UIControlState.normal because the type is inferred.

    0 讨论(0)
  • 2020-12-23 13:26

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

    0 讨论(0)
  • 2020-12-23 13:31

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

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

    Documentation: isEnabled, setTitle.

    Older code:

    myButton.enabled = false // disables
    myButton.setTitle("myTitle", forState: UIControlState.Normal) // sets text
    
    0 讨论(0)
  • 2020-12-23 13:32
    [myButton setTitle: @"myTitle" forState: UIControlStateNormal];
    

    Use UIControlStateNormal to set your title.

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

    [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];
    
    0 讨论(0)
  • 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;

    [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 IBAction calls:

    - (IBAction) triggerActionWithSender: (id) sender;
    

    This can be bound to the button and you’ll get the button in the sender argument 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:

    @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.

    0 讨论(0)
  • 2020-12-23 13:42

    Assuming that the button is a UIButton:

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

    See the documentation for UIButton.

    0 讨论(0)
提交回复
热议问题