繁体   English   中英

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

[英]Cannot assign value of type 'Bool' to type '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);
        }
    }
 }

你错过这里的设置

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 

profileVisibleUIButton类型,您需要将其设置为isHidden属性,而不是直接设置

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM