Swift我无法隐藏分段控件

时间:2019-01-22 05:54:31

标签: ios swift uisegmentedcontrol uialertcontroller

我有两个按钮,一个显示分段控件,另一个试图隐藏它。问题是当我单击一个显示它时,它可以工作。但是,当我单击隐藏它时,它不起作用。这是我的代码:

let delayHide = UIAlertAction(title: "Hide Delay", style: .default) { (action) in
    self.segmentedHidden = 1
    self.setupSegmented()
}

let delayShow = UIAlertAction(title: "Show Delay", style: .default) { (action) in
    self.segmentedHidden = 0
    self.setupSegmented()
}

这也是我尝试隐藏它的代码:

if (segmentedHidden == 0) {
    segmentedControl.isHidden = false
} else {
    segmentedControl.isHidden = true
}

我哪里出错了?

1 个答案:

答案 0 :(得分:0)

使用以下代码:

    @IBAction func buttonTapped(_ sender: UIButton) {

        let alert = UIAlertController(title: "Alert", message: "Segment", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "Show", style: .default, handler: { (alertAction) in
            self.showHideSegmentControl(isHidden: false)
        }))
        alert.addAction(UIAlertAction(title: "hide", style: .default, handler: { (alertAction) in
            self.showHideSegmentControl(isHidden: true)
        }))
        self.present(alert, animated: true, completion: nil)

    }

    func showHideSegmentControl(isHidden: Bool) {
        segmentedControl.isHidden = isHidden
    }