2016-07-18 17 views
0

我向上帝发誓我在这里有一个流氓变量 - 我有一个bool menuUp,它只是告诉你某些菜单按钮是否已经移动了。Swift - 布尔的价值被改变而没有做任何事情?不能找到它被改变的地方?

我首先声明bool为false,然后根据bool(如果菜单已启动或未启动),动画将在滑动手势/函数上运行。

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

我已经设置了许多断点和打印报表,但它好像在布尔正在没有我做任何修改。当应用程序加载时,我有我的超级视图控制器调用此函数,打印以下内容并使menuup为真。

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

所以它的真实。大。然后在另一个类我引用这个超级viecontorller这样和一个按钮尝试调用关机功能菜单,也可通过向下滑动超级视图控制器上叫做 -

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 而在调用上述函数的函数的开头我打印出了布尔值,这里也是假的。

奇怪的是,当我用滑动触发moveMenuDown时,它输出正确(开始为真,结束为假)。

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

因为它将ht bool读为false,所以它不调用动画并将其向下移动到moveMenuDown中。这是一个主要问题。

我能在这里做什么?我尝试点击放大镜找到所有我参考bool的地方,但是我不会在超级视图控制器以外的任何地方改变它,我只在这两个函数中改变了TWICE。

怎么回事?

编辑:

我现在在班上写在其他子视图控制器超级视图控制器和伊夫有weak var superv: StarterViewController!

let s = StarterViewController() 

,并在viewDidLoad中:

s.superv = self 

但得到错误enter image description here

+1

当初始化一个'StarterViewController',你确定你没有初始化'menuUp'的第二个实例? –

+0

你可以添加一个断点,当它的值发生变化时 – Gruntcakes

+0

@SausageDioxide我该怎么做?我不熟悉断点 – skyguy

回答

2

在这一行

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

你实际上是创造了一个StarterViewController例如!无论何时创建StarterViewController,其实例属性都会被初始化。其中之一是menuUp,它被初始化为false

你看到了吗?您创建的superv不是menuUp设置为true的控制器。这是StarterViewController的完全不同且独立的实例。

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

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

而且也,你应该改变的superv此声明:

weak var superv: StarterViewController! 
+0

谢谢。开始了解。你在哪里和在哪些课堂上宣布这些课程? – skyguy

+0

请看我的问题,我做了什么 - 需要一些帮助 – skyguy

+0

这很容易。只需将您当前的'superv'声明更改为上面的。你如何创建你提到的其他课程?在创建另一个类并将其放入一个名为'theOtherClass'的变量之后,写下'theOtherClass.superv = self' @skyguy – Sweeper

-2

添加属性的观察者像下面

var menuUp = Bool(false) { //at class level 
    didSet { 
     print("") 
    } 
} 

打印(“”)行添加一个调试点,看看谁在改变它。