选择

时间:2016-12-03 05:55:39

标签: ios swift uibutton

我有2个单选按钮男,女。我做了所有事情......当我尝试选择男性,女性radiobuton。它根本没有选择。这是我的代码:

 @IBOutlet var btn_male: UIButton! // create outlet for radio button
 @IBOutlet var btn_female: UIButton!

 @IBAction func btn_male_female_click(sender: UIButton) {
        switch sender.tag {
        case 10:
            //male select
            if (sender.selected == true) {
                sender.selected = false;
                btn_male.selected = true;
                btn_female.selected = false;
                gender = "1"
                print("male  selected")

            }else{
                sender.selected = true;
                btn_male.selected = false;
                btn_female.selected = true;
                gender = "2"
                 print("female  selected")
            }
            break;
        case 20:
            //female select
            if (sender.selected == true) {
                sender.selected = false;
                btn_female.selected = true;

                btn_male.selected = false;
                gender = "2"
                 print("female  selected")
            }else{
                sender.selected = true;
                btn_female.selected = false;
                btn_male.selected = true;
                gender = "1"
                 print("male  selected")
            }

            break;
        default:
            break;
        }
    }

我不知道为什么不选择。要更改图像,我将状态配置更改为选中,并保留选定的无线电图像。

但我的问题是它根本没有选择。请帮帮我。谢谢

更新

我更新此解决方案后。我只是从我的数据库检查,我正在讨论他们已经保存的特定用户的性别。但我有一个问题。

例如,如果我选择男性并保存它。当我返回到我的个人资料屏幕时,我的男性按钮显示为已选中。这来自db没有问题。但是,当我想将单选按钮从男性更改为女性时,以及当我按下女性单选按钮时。我的男性单选按钮仍然是选中的,它不会改变为取消选择。如果我这样做两次然后改变它。

 @IBAction func btn_male_female_click(sender: UIButton) {
        self.selectedButton.selected = false
        self.selectedButton = sender
        self.selectedButton.selected = true
        gender = "male"
        if sender == btn_female {
            gender = "female"
        }
        else  if sender == btn_male {

            gender = "male"
        }
}
 override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(true)

 if profileData.Gender .isEmpty {





        }else{
            if profileData.Gender == "male"
            {
                self.btn_male.selected = true
                self.btn_female.selected = false
            }
            else if profileData.Gender == "female"
            {
                self.btn_male.selected = false
                self.btn_female.selected = true
            }

        }
}

2 个答案:

答案 0 :(得分:2)

你可以通过简单地创建另一个UIButton实例来解决这个问题,该实例在这两个按钮之间保存所选按钮。

@IBOutlet var btn_male: UIButton! // create outlet for radio button
@IBOutlet var btn_female: UIButton!
var selectedButton = UIButton()

现在使用这样的按钮动作。

@IBAction func btn_male_female_click(sender: UIButton) {
    self.selectedButton.selected = false
    self.selectedButton = sender 
    self.selectedButton.selected = true
    gender = "1" 
    if sender == btn_female {
         gender = "2"
    }
}

修改:您可以像这样管理所选属性。

if !(profileData.Gender.isEmpty) {

    if profileData.Gender == "male"
    {
         self.selectedButton = self.btn_male
    }
    else if profileData.Gender == "female"
    {
         self.selectedButton = self.btn_female             
    }
    self.selectedButton.selected = true
}

答案 1 :(得分:1)

在你的代码问题是

case 10:
        //male select
        if (sender.selected == true) {
            sender.selected = false; // btn_male selected "false"
            btn_male.selected = true; // btn_male selected "true" that's the problem
            btn_female.selected = false;
            gender = "1"
            print("male  selected")

        }

我希望此代码可以帮助您

@IBAction func btn_male_female_click(sender: UIButton) {
    switch sender.tag {
    case 10:
        //male select
        if (sender.selected == true) {
            sender.selected = false;
            btn_female.selected = false;
            gender = "1"
            print("male  selected")

        }else{
            sender.selected = true;
            btn_female.selected = true;
            gender = "2"
            print("female  selected")
        }
        break;
    case 20:
        //female select
        if (sender.selected == true) {
            sender.selected = false;
            btn_male.selected = false;
            gender = "2"
            print("female  selected")
        }else{
            sender.selected = true;
            btn_male.selected = true;
            gender = "1"
            print("male  selected")
        }

        break;
    default:
        break;
    }
}