skyguy skyguy - 4 months ago 7
Swift Question

Swift - value of bool being changed without doing anything? Cant find where its being altered?

I swear to god I have a rogue variable here - I have a bool

menuUp
that simply tells whether some menu buttons have been moved up or not.

I first declare the bool as false then depending on the bool (if the menu is up or not), an animation will run on a swipe gesture/function.

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


I have set numerous breakpoints and print statements but it seems as though the bool is being modified without me doing anything. When the app loads I have my super view controller call this function, printing the following and making
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

So its true. Great. THEN in another class I reference this super viecontorller like this and on a button try to call the menu down function, which can also be called by swiping down on the super view controller -

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


Then

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)

}


which calls

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)

}


And this function IS CALLEd however it prints -
enter image description here
And at the beginning of the function that calls the above func Ive printed out the bool and it is false here also.

Oddly, when I trigger
moveMenuDown
with a swipe it outputs correctly (starts true, ends false).

The menu is still clearly up I can see it so I KNOW the bool should not/is not false.

Because it is reading ht bool as false, it does not call the animation and move th menu down in
moveMenuDown
. This is a major problem.

What can I do here? Ive tried clicking the magnify glass to find all the places I reference the bool but I don't change it ANYWHERE outside the super view controller and I only changed it TWICE in those 2 functions.

Whats going on?

EDIT:

I now have
weak var superv: StarterViewController!
in the class of the super view controller and Ive written in the other, sub view controller:

let s = StarterViewController()


and in viewdidLoad:

s.superv = self


but getting the error enter image description here

Answer

In this line

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

You are actually creating a new instance of StarterViewController! Whenever a StarterViewController is created, its instance properties get initialized. One of them is menuUp, which gets initialized to false.

Do you see that? The superv you created is not the controller which has menuUp set to true earlier. It's a completely different and independent instance of StarterViewController.

To fix this, you need to assign self to superv after you created the "another class" that you were talking about.

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

And also, you should change the declaration of superv to this:

weak var superv: StarterViewController!