Label hide very slow

Code create label and button that hide the label. But when run code and press button to hide label, it take long time to hide. Please help me, how to fix this problem?


import UIKit


class ViewController: UIViewController {

@IBOutlet var label: UILabel!

override func viewDidLoad( ) {


super.viewDidLoad ( )

label = UILabel(frame: CGRect(x: 30, y: 30, width: 300, height: 30))

label.text = "Hey"

label.textColor = UIColor.black

self.view.addSubview(label)


let button = UIButton(frame: CGRect(x: 40, y: 500, width: 300, height: 40))

button.setTitle.setTitle("Change label text", for: .normal)

button.setTitleColor(UIColor.black, for: .normal)

button.backgroundColor = UIColor.gray

button.addTarget(self, action: #selector(hide), for: .touchUpInside)

self.view.addSubview(button)

}

@IBAction func hide(_ sender: Any){

self.label.isHidden = true

print("Check")

}

}



I also set print "Check" on action to see button have been activate or not. Once I press button, text "Check" show but button very slow to hide (take around 15 sec). Any advise to fix this problem?

I tested and made it work with no time lag (tested on simulator, XCode 10.3).


But there are many issues with your code:


import UIKit

class ViewController: UIViewController {
    @IBOutlet var label: UILabel!
     override func viewDidLoad( ) {

     super.viewDidLoad ( )
     label = UILabel(frame: CGRect(x: 30, y: 30, width: 300, height: 30))
     label.text = "Hey"
     label.textColor = UIColor.black
     self.view.addSubview(label)

     let button = UIButton(frame: CGRect(x: 40, y: 500, width: 300, height: 40))
     button.setTitle.setTitle("Change label text", for: .normal)
     button.setTitleColor(UIColor.black, for: .normal)
     button.backgroundColor = UIColor.gray
     button.addTarget(self, action: #selector(hide), for: .touchUpInside)
     self.view.addSubview(button)
     }
     @IBAction func hide(_ sender: Any){
          self.label.isHidden = true
          print("Check")
     }
}



Line 4: you define an IBOutlet. Have you included a label in Interface Builder view ?

If so where ?

This is useless, as you create the label in code

Replace line 4 with

     var label: UILabel!


Line 14: this cannot compile. Correct code is

        button.setTitle("Change label text", for: .normal)



Line 18: you define an IBAction, but button is defined in code.

It works, but better code:

    @objc func hide(_ sender: Any){


So, the complete code becomes


class ViewController: UIViewController {
    var label: UILabel!

    override func viewDidLoad( ) {
       
        super.viewDidLoad ( )
        label = UILabel(frame: CGRect(x: 30, y: 30, width: 300, height: 30))
        label.text = "Hey"
        label.textColor = UIColor.black
        self.view.addSubview(label)
       
        let button = UIButton(frame: CGRect(x: 40, y: 500, width: 300, height: 40))
        button.setTitle("Change label text", for: .normal)
        button.setTitleColor(UIColor.black, for: .normal)
        button.backgroundColor = UIColor.gray
        button.addTarget(self, action: #selector(hide), for: .touchUpInside)
        self.view.addSubview(button)
    }
//    @IBAction
    @objc func hide(_ sender: Any){
        self.label.isHidden = true
        print("Check")
    }
}



Finally, you would make your life easier by creating the objects in IB, not in code

Thank you for your advise. I'm new guy for this forum that I can not copy/paste code into here. So it has some typo here.


I'm copy your advise code to run on xcode. I still got issue for label hide not immediately. Could you please tell that this code run simulator on your xcode have same problem or not?

Yes, I ran the code on simulator (XCode 10.3).


Copy and paste is immediate. If you have problem, use the Edit > Paste and Match style menu option.


It works without any problem: label hides instantaneously.

I do not see any reason for your problemof 15 seconds delay.

Are you sure you have copied the exact code from my post ?

What is your version of XCode ?


Try a few things:

- do an option-clean build folder (Product menu)

- if not OK, restart XCode

- if not OK, restart the Mac.


I have tested with a different show, which is in fact showHide;


    @objc func hide(_ sender: Any) {
        self.label.isHidden = !self.label.isHidden // true
        print("Check")
    }

Works OK as well of course.

I use xcode 10.1 due no longer support new version on my Macbook pro 2011.

I suspect it might be bug on my xcode. Thank you to help to me confirm!

Just tested on XCode 10.1 beta. Swift 4.2.


Works OK.


Please confirm you have copied EXACTLY the code I sent yesterday:




class ViewController: UIViewController { 
    var label: UILabel! 
 
    override func viewDidLoad( ) { 
        
        super.viewDidLoad ( ) 
        label = UILabel(frame: CGRect(x: 30, y: 30, width: 300, height: 30)) 
        label.text = "Hey" 
        label.textColor = UIColor.black 
        self.view.addSubview(label) 
        
        let button = UIButton(frame: CGRect(x: 40, y: 500, width: 300, height: 40)) 
        button.setTitle("Change label text", for: .normal) 
        button.setTitleColor(UIColor.black, for: .normal) 
        button.backgroundColor = UIColor.gray 
        button.addTarget(self, action: #selector(hide), for: .touchUpInside) 
        self.view.addSubview(button) 
    } 

    @objc func hide(_ sender: Any){ 
        self.label.isHidden = true 
        print("Check") 
    } 
}

Yes, I copy all of your code but the result still delay disppear. If repeat press button two time, the label will this appear on second press. Very strange. The button that create by main.storyboard will do not got this issue.

I try to feedback to apple but my account un-able to log in

Label hide very slow
 
 
Q