无法将类型“布尔”的值分配给类型“ UIButton?”

时间:2019-06-16 23:16:21

标签: ios swift xcode uibutton

我试图隐藏一个按钮,直到满足其他条件。当我加载应用程序时,我正在使用滑块,当滑块达到最大值时,它应该使按钮可见,但是出于某些奇怪的原因,它不会。我刚刚在另一个没有问题的应用程序中完成了该操作,并且代码几乎相同。

不知道该怎么办。

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated) 
    profileVisible.isHidden = true
}

@IBAction func btnClicked(_ sender: Any) { 

    motionManager.startDeviceMotionUpdates(to: queue) { (motion, error) in DispatchQueue.main.async {
        self.slider.value = Float((motion?.attitude.roll ?? 0 ) * 1.4)

        print(self.slider.value)

        if self.slider.value == 1.0 {
            self.profileVisible = false
            self.motionManager.stopDeviceMotionUpdates()


            AudioServicesPlayAlertSound(kSystemSoundID_Vibrate); // vibrates when payment has succeded
        }

        if self.slider.value == -1.0 {
           profileVisible = true
            self.motionManager.stopDeviceMotionUpdates()
            AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
        }
    }
 }

1 个答案:

答案 0 :(得分:1)

您错过了此处的设置

if self.slider.value == 1.0 {
    self.profileVisible = false

还有

if self.slider.value == -1.0 {
     profileVisible = true

应该是

profileVisible.isHidden = true/false // set it's value according to your logic 

profileVisible的类型为UIButton,您需要直接设置其isHidden属性