2

Using Swift, I have a UILabel that was created programmatically in the viewDidLoad override function:

var middleDate = UILabel(frame: CGRect(x: (screenWidth / 2) - 45, y: 0, width: 90, height: 20))
    middleDate.text = "3:13:46 PM"
    middleDate.font = UIFont(name: "ArialMT", size: 13)
    middleDate.textAlignment = .Center

Which works great.

Then I am trying to change the text in the override function viewDidAppear. I get Build Failed with the message Use of unresolved identifier 'middleDate'

If I use interface builder and drag and drop a UILabel on the view, then give it a strong reference it works great. Is there a way to reference a programmatically created object with a strong reference?

Or am I going about this incorrect?


This is what I have on my ViewController.swift:

I have a feeling that there is a much better and cleaner way to do this.

override func viewDidLoad() {

    super.viewDidLoad()

    var middleDate = UILabel(frame: CGRect(x: (screenWidth / 2) - 45, y: 0, width: 90, height: 20))
    middleDate.text = "3:13:46 PM"
    middleDate.font = UIFont(name: "ArialMT", size: 13)
    middleDate.textAlignment = .Center

    self.myTopView.addSubview(middleDate)
}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    var timer = NSTimer.scheduledTimerWithTimeInterval(1.0,
        target: self,
        selector: Selector("getLocalTime:"),
        userInfo: nil,
        repeats: true)

    let mainLoop = NSRunLoop.mainRunLoop()
    mainLoop.addTimer(timer, forMode: NSDefaultRunLoopMode)

    func getLocalTime(timer: NSTimer) { 

        var todaysDate:NSDate = NSDate()
        var dateFormatter:NSDateFormatter = NSDateFormatter()
        var centerDateFormatter:NSDateFormatter = NSDateFormatter()

        dateFormatter.dateFormat = "h:mm:ss"            

        var DateInFormat:String = dateFormatter.stringFromDate(todaysDate)            

        middleDate.text = "hello"


    }
}
3
  • Have a class memner: "var middleDate: UILabel?". Use same in viewDidLoad and viewDidAppear. Commented Oct 22, 2014 at 22:21
  • I am adding 'var middleDate: UILabel?' to the viewDidLoad and the viewDidAppear and I am getting errors. errors: UILabel does not have a member named 'text'. And where is created the UILabel and added to the sub view I am also getting errors Commented Oct 22, 2014 at 23:22
  • The code you provided does not display a UILabel on the view. It makes a local variable containing a UILabel which is reinitialized as soon as viewDidLoad exits. If you are adding the label to your view at some later point, how are you getting a reference to the label in your viewWillAppear? You don't show that code. In short, we need to see more code. Commented Oct 22, 2014 at 23:51

1 Answer 1

3

Try this:

class ViewController: UIViewController {

    var middleDate = UILabel(frame: CGRect(x: (360 / 2) - 45, y: 200, width: 90, height: 20))

    override func viewDidLoad() {

        super.viewDidLoad()

        middleDate.text = "3:13:46 PM"
        middleDate.font = UIFont(name: "ArialMT", size: 13)
        middleDate.textAlignment = .Center

        self.view.addSubview(middleDate)
    }

    override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated)

        middleDate.text = "hello"
    }
}

Your issue was middleDate was declared with local scope within the viewDidLoad function. In order for it to be a class variable you need to declare it at the class level. I simplified the class a little just to make it work quickly for me.

Sign up to request clarification or add additional context in comments.

1 Comment

for a few unique Labels that's the way to go, but if i was creating an array of UILabels to be iterated over as part of an update, then that's beyond my knowledge

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.