在swift中切换机箱

时间:2016-11-08 09:55:48

标签: ios swift uibutton switch-statement

这些是我的UIButtons

@IBOutlet weak var btnProgressDate: UIButton!
@IBOutlet weak var btnClosedDate: UIButton!

我已将这两个按钮拖动为:

@IBAction func btnClicked(sender: UIButton) {
    let tag = sender.tag
    self.clearPopups()
    switch (tag) {
            case 1:
        self.datePicker.hidden = false
        self.datePicker.datePicker.maximumDate = nil
        self.datePicker.datePicker.minimumDate = nil

        if(self.healthActionPlanCondition.dateClosed != nil)
        {
            self.datePicker.datePicker.maximumDate = self.healthActionPlanCondition.dateClosed
        }
        if(self.healthActionPlanCondition.nextProgressReviewDate != nil)
        {
            self.datePicker.datePicker.setDate(self.healthActionPlanCondition.nextProgressReviewDate, animated: true)
        }
        self.selectedBtn = self.btnProgressDate
    case 2:
        self.datePicker.hidden = false
        if(self.healthActionPlanCondition.nextProgressReviewDate != nil)
        {
            self.datePicker.datePicker.minimumDate = self.healthActionPlanCondition.nextProgressReviewDate
        }

        if(self.healthActionPlanCondition.dateClosed != nil)
        {
            self.datePicker.datePicker.setDate(self.healthActionPlanCondition.dateClosed, animated: true)
        }
        self.datePicker.datePicker.maximumDate = nil
        self.selectedBtn = self.btnClosedDate
    default:
        print("Proper btn not found")
    }


}

但每次点击任何按钮默认的情况都会被执行。我对迅捷是全新的。我在这做错了什么?

4 个答案:

答案 0 :(得分:2)

我确定这完全是显而易见的,但实际上是否为按钮设置了标签属性?你的代码看起来对我来说完全没问题,所以我无法想到你遇到这个问题的任何其他原因。

答案 1 :(得分:1)

而不是使用tag,在您的操作方法中,可以使用您创建的IBOutlet UIButton

@IBAction func btnClicked(sender: UIButton) {
    self.clearPopups()
    if sender == btnProgressDate {
        self.datePicker.hidden = false
        self.datePicker.datePicker.maximumDate = nil
        self.datePicker.datePicker.minimumDate = nil

        if(self.healthActionPlanCondition.dateClosed != nil)
        {
            self.datePicker.datePicker.maximumDate = self.healthActionPlanCondition.dateClosed
        }
        if(self.healthActionPlanCondition.nextProgressReviewDate != nil)
        {
            self.datePicker.datePicker.setDate(self.healthActionPlanCondition.nextProgressReviewDate, animated: true)
        }
        self.selectedBtn = self.btnProgressDate
    }
    else if sender == btnClosedDate {
        self.datePicker.hidden = false
        if(self.healthActionPlanCondition.nextProgressReviewDate != nil)
        {
            self.datePicker.datePicker.minimumDate = self.healthActionPlanCondition.nextProgressReviewDate
        }

        if(self.healthActionPlanCondition.dateClosed != nil)
        {
            self.datePicker.datePicker.setDate(self.healthActionPlanCondition.dateClosed, animated: true)
        }
        self.datePicker.datePicker.maximumDate = nil
        self.selectedBtn = self.btnClosedDate
    }
    else {
        print("Proper btn not found")
    }
}    

答案 2 :(得分:1)

@IBAction func btnClicked(sender: UIButton) {
    self.clearPopups()
    switch sender {
    case btnProgressDate:
        self.datePicker.hidden = false
        self.datePicker.datePicker.maximumDate = nil
        self.datePicker.datePicker.minimumDate = nil

        if(self.healthActionPlanCondition.dateClosed != nil)
        {
            self.datePicker.datePicker.maximumDate = self.healthActionPlanCondition.dateClosed
        }

        if(self.healthActionPlanCondition.nextProgressReviewDate != nil)
        {
            self.datePicker.datePicker.setDate(self.healthActionPlanCondition.nextProgressReviewDate, animated: true)
        }
        self.selectedBtn = self.btnProgressDate
    case btnClosedDate:
        self.datePicker.hidden = false
        if(self.healthActionPlanCondition.nextProgressReviewDate != nil)
        {
            self.datePicker.datePicker.minimumDate = self.healthActionPlanCondition.nextProgressReviewDate
        }

        if(self.healthActionPlanCondition.dateClosed != nil)
        {
            self.datePicker.datePicker.setDate(self.healthActionPlanCondition.dateClosed, animated: true)
        }
        self.datePicker.datePicker.maximumDate = nil
        self.selectedBtn = self.btnClosedDate
    default:
        print("Proper btn not found")
    }


}

答案 3 :(得分:0)

您不需要初始设置标记0.只需将标记1设置为第一个按钮,将标记2设置为第二个按钮。