iOS中的UIButton选择事件

时间:2013-09-23 05:32:50

标签: ios uibutton

我正在开发一个iPad应用程序,我有三个按钮,分别是Button1,Button2和Button3。 Button1和Button2是加载数据两个不同的标签,当我们点击第三个按钮(Button3)时,根据这两个按钮选择,要显示的所选按钮(按钮1或按钮2)的标签值将被显示。

-(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";
    }
}

我该怎么做?任何想法都会有所帮助。

2 个答案:

答案 0 :(得分: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 :(得分: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";
    }
}

希望有所帮助!