在iOS中更改button的文本并禁用button

你如何改变button的文本,并禁用iOS中的button?

嘿Namratha,如果你想要改变文本和UIButton的启用/禁用状态,可以很容易地完成,如下所示:

[myButton setTitle:@"Normal State Title" forState:UIControlStateNormal]; // To set the title [myButton setEnabled:NO]; // To toggle enabled / disabled 

如果您在Interface Builder中创build了这些button,并且想要通过代码访问这些button,则可以利用它们作为参数传入IBAction调用的事实:

 - (IBAction) triggerActionWithSender: (id) sender; 

这可以绑定到button,当触发操作时,您将获得sender参数中的button。 如果这还不够(因为你需要访问别的地方的button而不是动作),为这个button声明一个sockets:

 @property(retain) IBOutlet UIButton *someButton; 

然后,可以将IB中的button绑定到控制器上,NIB加载代码将在加载接口时设置属性值。

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

如果谁在斯威夫特寻找解决scheme,在这里降落,那将是:

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

假设这个button是一个UIButton

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

请参阅UIButton的文档。

要更改button标题:

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

对于禁用:

 [mybtn setEnabled:NO]; 

在Swift 3中,你可以简单地通过下面的方式改变一个button的标题:

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

并通过以下方式禁用button:

 button.isEnabled = false 

.normalUIControlState.normal相同,因为types是推断的。

如果你想改变标题作为被点击的响应,你可以在你的视图控制器代理中的button的IBAction方法内尝试这个。 这打开和closures了语音聊天。 设置语音聊天不包括在这里!

 - (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当然是特定于语音聊天的,但是您可以使用您的ow本地布尔属性来控制交换机。