2011-02-10 246 views

回答

196

嘿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参数中的按钮。如果这还不够(因为你需要访问按钮其他地方比在行动),申报出口的按钮:

@property(retain) IBOutlet UIButton *someButton; 

然后,它可以将按钮IB绑定到控制器,笔尖装代码将在加载接口时设置属性值。

+0

谢谢!我在我的应用程序中有UIButtons,但是我没有在任何地方的代码中提到它们。我使用了接口构建器来注册目标动作机制(为此,我有几个IBAction方法来处理按钮点击),所以我如何访问按钮? – Namratha 2011-02-11 04:18:02

+1

刚才我已经意识到我正在编辑你的答案,MGunetileke,不是我的:)希望没关系。 – zoul 2011-02-11 07:07:07

+1

也想补充一点,如果你想要为你声明@属性。在XCode 4中,按住CTRL键,单击按钮,然后将鼠标拖入视图的相应.h文件中。弹出对话框提示您输入属性名称。然后瞧,你会有你的代码中使用的财产! – milesmeow 2011-12-24 01:01:53

9

假设按钮是一个UIButton

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

查看的文档UIButton

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

如果有人,谁是寻找在斯威夫特的解决方案,在这里降落,这将是:

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

更改按钮标题:

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

对于禁用:

[mybtn setEnabled:NO]; 
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]; 
} 

}

有空视频是针对语音,当然聊,但您可以使用您的流本地布尔属性来控制开关。

2

在斯威夫特3,你可以简单地改变该按钮的标题:

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

,你通过禁用按钮:

button.isEnabled = false 

.normal相同UIControlState.normal,因为该类型推断。