2011-02-10 66 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,因爲該類型推斷。