change text of button and disable button in iOS

后端 未结 8 892
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

    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.

提交回复
热议问题