ios - Swift - bool 的值被改变而不做任何事情?找不到修改的地方吗?

标签 ios swift

我向上帝发誓,我这里有一个流氓变量 - 我有一个 bool menuUp ,它只是告诉某些菜单按钮是否已向上移动。

我首先将 bool 值声明为 false,然后根据 bool 值(菜单是否打开),动画将在滑动手势/功能上运行。

var menuUp = Bool(false) //at class level

我设置了许多断点和打印语句,但似乎 bool 值在我不做任何事情的情况下就被修改了。当应用程序加载时,我让我的 super View Controller 调用此函数,打印以下内容并使 menuup true。

func moveMenuUp(sender: AnyObject!)
    {
        print("menu up:")
        print(menuUp)

        if !menuUp && !externalViewUp
        {
            UIView.animateWithDuration(0.8, delay: 0.0, usingSpringWithDamping:
                0.65, initialSpringVelocity: 1.2, options: [], animations: {

                    //above change the duration to the time it will take,
                    //and fiddle with the springs between 0-1 until you are happy with the effect.
                    self.menuBtn1.center.y -= screenSize.height * (122/568)
                    self.menuBtn2.center.y -= screenSize.height * (122/568)
                    self.menuBtn3.center.y -= screenSize.height * (122/568)
                    //chnage frame however you want to here

                }, completion: { finished in
                    //code that runs after the transition is complete here

            })
            menuUp = !menuUp
        }

        print(menuUp)

    }

enter image description here

所以这是真的。伟大的。然后在另一个类中,我像这样引用这个 super viecontorller,并在按钮上尝试调用菜单向下函数,也可以通过在 super View Controller 上向下滑动来调用该函数 -

let superv : StarterViewController = StarterViewController (nibName: "StarterViewController", bundle: nil)

然后

func showIdeaView()
    {
        networkTimer.invalidate()

         superv.moveMenuDown(self) //THIS NEEDS WORK!! BOOL ISSUES!

        externalViewUp = true

        viewGestureRecognizer.enabled = false
        self.addChildViewController(v)
        self.view.addSubview(v.view)
        v.didMoveToParentViewController(self)

        v.view.frame = self.view.bounds
        self.view.bringSubviewToFront(v.view)

    }

调用

func moveMenuDown(sender: AnyObject!)
    {
        print("moving down!!!")
        print(menuUp)

        if menuUp == true {
            UIView.animateWithDuration(0.8, delay: 0.0, usingSpringWithDamping:
                0.65, initialSpringVelocity: 1.2, options: [], animations: {

                    //above change the duration to the time it will take,
                    //and fiddle with the springs between 0-1 until you are happy with the effect.
                    self.menuBtn1.center.y += screenSize.height * (122/568)
                    self.menuBtn2.center.y += screenSize.height * (122/568)
                    self.menuBtn3.center.y += screenSize.height * (122/568)
                    //chnage frame however you want to here

                }, completion: { finished in
                    //code that runs after the transition is complete here

            })
        menuUp = !menuUp
            }

        print(menuUp)

    }

这个函数被调用,但它打印 - enter image description here 在调用上述 func 的函数的开头,我打印出了 bool,这里也是 false。

奇怪的是,当我通过滑动触发 moveMenuDown 时,它会正确输出(开始为 true,结束为 false)。

菜单仍然清晰可见,我可以看到它,所以我知道 bool 值不应该/不是假的。

因为它读取 ht bool 为 false,所以它不会调用动画并在 moveMenuDown 中向下移动菜单。这是一个大问题。

我在这里能做什么?我尝试单击放大镜来查找我引用 bool 的所有位置,但我没有在 super View Controller 之外的任何地方更改它,并且我只在这两个函数中更改了两次。

发生什么事了?

编辑:

我现在在 super View Controller 的类中有weak var superv: StarterViewController!,并且我已经在另一个 subview Controller 中编写了:

 let s  = StarterViewController()

并在viewdidLoad中:

s.superv = self 

但出现错误enter image description here

最佳答案

在这一行

let superv : StarterViewController = 
    StarterViewController (nibName: "StarterViewController", bundle: nil)

您实际上正在创建 StarterViewController实例!每当创建 StarterViewController 时,它的实例属性就会被初始化。其中之一是 menuUp,它被初始化为 false

你看到了吗?您创建的 superv 不是之前将 menuUp 设置为 true 的 Controller 。它是一个完全不同且独立的 StarterViewController 实例。

要解决此问题,您需要在创建您正在讨论的“另一个类”后将 self 分配给 superv

let theOtherClass = TheOtherClass(...)
theOtherClass.superv = self

而且,您应该将 superv 的声明更改为:

weak var superv: StarterViewController!

关于ios - Swift - bool 的值被改变而不做任何事情?找不到修改的地方吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38427901/

相关文章:

iOS - 未调用警报 View 的 segue

ios - NSUserDefaults 突然停止保存数据

ios - 单例重叠?

Swift - 旋转图像,然后放大并裁剪以保持宽度/高度纵横比不变

iOS:之前已经使用过应用程序时跳过 ViewControllers

ios - 如何查找向 canPerformAction :withSender: 返回 YES 的响应者

iPhone:语音识别在 IOS SDK 中可用吗?

ios - 以键和值数组的形式快速从 JSON 中检索特定数组?

swift - 如何在 MacOS 上为 VoiceOver 在 Swift 中对 UI 元素进行分组?

ios - 读取加速度计数据时发现 nil