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

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
}


我哪里做错了?

最佳答案

使用以下代码:

    @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
    }

10-07 19:18
查看更多