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"
}
}