2013-09-23 61 views
-1

我正在研究iPad應用程序,其中有三個按鈕Button1,Button2和Button3。 Button1和Button2用於加載數據兩個不同的標籤,當我們點擊第三個按鈕(Button3)時,基於這兩個按鈕選擇,將顯示與所選按鈕(按鈕1或按鈕2)相對應的標籤值。iOS中的UIButton選擇事件

-(IBAction)btnCustomer 
{ 
    SelectedButton.text = @"Customer"; 
} 

-(IBAction)btnBranch 
{ 
    SelectedButton.text = @"Branch"; 
} 

-(IBAction)btnDisplay 
{ 
    if(btnCustomer.selected == TRUE) 
    { 
     TitleLabel.text = @"btnCustomert is Selected"; 
    } 
    else if(btnBranch.selected == TRUE) 
    { 
     TitleLabel.text = @"btnBranch is Selected"; 
    } 
} 

我該怎麼做?任何想法都會有所幫助。

+1

我找不出你的問題,並請爲下一個問題嘗試更好地格式化您的代碼。 – null

+0

是的。清楚地解釋問題。 – Ganapathy

回答

2

我想這會幫助你。 。 。

-(IBAction)btnCustomer 
{ 
    SelectedButton.text = @"Customer"; 

    btnCustomer.selected = ! ButttonCustomer.selected; 

} 
-(IBAction)btnBranch 
{ 
    SelectedButton.text = @"Branch"; 

    btnBranch.selected = ! btnBranch.selected; 
} 

-(IBAction)btnDisplay 
{ 
    if(btnCustomer.selected) 
    { 
     TitleLabel.text = @"btnCustomert is Selected"; 
     btnCustomer.selected = ! ButttonCustomer.selected; 

    } 
    else if(btnBranch.selected) 
    { 
     TitleLabel.text = @"btnBranch is Selected"; 
     btnBranch.selected = ! btnBranch.selected; 
    } 
} 
1

正在檢查以確定是否選擇或不按鈕selected屬性,需要通過你的代碼中設置/復位。

Apple docs: UIControlUIButton超類)

指定如果選擇了控制YES;否則不是

另外,IBAction方法簽名不正確。它需要有一個參數sender(即發送此動作消息的按鈕實例)。

這裏是修改的代碼。

-(IBAction)btnCustomer:(id)sender 
{ 
    UIButton *btn = (UIButton*) sender; 
    SelectedButton.text = @"Customer"; 
    //Toggle selected state 
    btn.selected = !btn.selected; 
} 

-(IBAction)btnBranch:(id)sender 
{ 
    UIButton *btn = (UIButton*) sender; 
    SelectedButton.text = @"Branch"; 
    //Toggle selected state 
    btn.selected = !btn.selected; 
} 

-(IBAction)btnDisplay:(id)sender 
{ 
    if(btnCustomer.selected == TRUE) 
    { 
     TitleLabel.text = @"btnCustomert is Selected"; 
    } 
    else if(btnBranch.selected == TRUE) 
    { 
     TitleLabel.text = @"btnBranch is Selected"; 
    } 
} 

希望有幫助!