2016-07-18 18 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("") 
    } 
} 

打印(「」)行添加一個調試點,看看誰在改變它。