【问题标题】:Cannot assign value of type 'Bool' to type 'UIButton?'无法将“Bool”类型的值分配给“UIButton”类型?
【发布时间】:2019-06-16 23:16:21
【问题描述】:

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

不知道该怎么办。

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);
        }
    }
 }

【问题讨论】:

    标签: ios swift xcode uibutton


    【解决方案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 
    

    profileVisibleUIButton 类型,你需要设置它的isHidden 属性而不是直接设置

    【讨论】:

    • 哇,我忘了 ishidden...过去 10 个小时一直在编码,感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多