Bug in Xcode when the text is put into a Label

2019-09-12 00:20发布

问题:

Here's my situation:

There are 8 ImageViews. The 4 on the left are called in my code (starting at the top): Left1, Left2, Left3, left4. The 4 on the right (always starting at the top): Right1, Right2, Right3, Right4.

Then there are two red Buttons in the middle: "Up" and "Down" and a Label.

What should happen is that when the Button Up is pressed, all the ImageViews go to a position which is above the top of the screen (so their center.y < 0) that I've set in the code, and when this happens the label text is set to "Up". When on the contrary I press the Button Down, all the ImageViews go back to the initial position and the label text is set to "Down".

What happens instead is that the label.text changes according to my code, but my ImageViews don't move.

Here's my code:

First i declare the objects:

class ViewController: UIViewController {
    @IBOutlet weak var Up: UIButton!
    @IBOutlet weak var Down: UIButton!
    @IBOutlet weak var Lab: UILabel!

    @IBOutlet weak var Left1: UIImageView!
    @IBOutlet weak var Left2: UIImageView!
    @IBOutlet weak var Left3: UIImageView!
    @IBOutlet weak var Left4: UIImageView!
    @IBOutlet weak var Right1: UIImageView!
    @IBOutlet weak var Right2: UIImageView!
    @IBOutlet weak var Right3: UIImageView!
    @IBOutlet weak var Right4: UIImageView!

In the View Did Load I hide all the ImageViews:

Left1.hidden = true
Left2.hidden = true
Left3.hidden = true
Left4.hidden = true
Right1.hidden = true
Right2.hidden = true
Right3.hidden = true
Right4.hidden = true

Then there are the two Buttons actions:

-Button Up

 @IBAction func Up(sender: AnyObject) {

            Lab.text = "Up"

            //-------- 1 ----------//
            Right1.center.y = 0 - 31
            Left1.center.y = 0 - 31
            Right1.hidden = false
            Left1.hidden = false

            //-------- 2 ----------//
            Right2.center.y = 0 - 300
            Left2.center.y = 0 - 300
            Right2.hidden = false
            Left2.hidden = false

            //-------- 3 ----------//
            Right3.center.y = 0 - 650
            Left3.center.y = 0 - 650
            Right3.hidden = false
            Left3.hidden = false

            //-------- 4 ----------//
            Right4.center.y = 0 - 960
            Left4.center.y = 0 - 960
            Right4.hidden = false
            Left4.hidden = false     
        }

Here I put the Label text to "Up" and, for every couple of ImageView (when I say couple I mean that the ImageViews at the top of the image (1) are the Couple 1, the ones at the bottom the Couple 4) I set their center.y to a value above the screen and then i show them.

-Button Down

@IBAction func Down(sender: AnyObject) {

        Lab.text = "Down"

        //-------- 1 ----------//
        Right1.center.y = 92
        Left1.center.y = 92

        //-------- 2 ----------//
        Right2.center.y = 171
        Left2.center.y = 171       

        //-------- 3 ----------//
        Right3.center.y = 249
        Left3.center.y = 249

        //-------- 4 ----------//
        Right4.center.y = 326
        Left4.center.y = 326

    }

Here i just put the label text to "down" and the ImageViews back to the initial positions.

Now, this should work perfectly, but what happens is that the label text is changed to up when I press the Up Button, but the ImageViews don't move, just appear.

On the contrary if I comment the label instructions then it works:

-Button Up

@IBAction func Up(sender: AnyObject) {

                //Lab.text = "Up"

                //-------- 1 ----------//
                Right1.center.y = 0 - 31
                Left1.center.y = 0 - 31
                Right1.hidden = false
                Left1.hidden = false

                //-------- 2 ----------//
                Right2.center.y = 0 - 300
                Left2.center.y = 0 - 300
                Right2.hidden = false
                Left2.hidden = false

                //-------- 3 ----------//
                Right3.center.y = 0 - 650
                Left3.center.y = 0 - 650
                Right3.hidden = false
                Left3.hidden = false

                //-------- 4 ----------//
                Right4.center.y = 0 - 960
                Left4.center.y = 0 - 960
                Right4.hidden = false
                Left4.hidden = false     
            } 

-Button Down

 @IBAction func Down(sender: AnyObject) {

            //Lab.text = "Down"

            //-------- 1 ----------//
            Right1.center.y = 92
            Left1.center.y = 92

            //-------- 2 ----------//
            Right2.center.y = 171
            Left2.center.y = 171       

            //-------- 3 ----------//
            Right3.center.y = 249
            Left3.center.y = 249

            //-------- 4 ----------//
            Right4.center.y = 326
            Left4.center.y = 326

        }

In this way the ImageViews move as they should, but of course the label text doesn't change.

Is there any relationship between the label and the imageviews?

I really hope you can help me, this is crazy.

Thank you