I'm new to learning Swift, and am trying to get an incredibly simple app to run. All I'm trying to do is get UIView.drawRect to update when I press a button. It updates/draws when the app first loads, and then nothing after that, whatever I try. I've been hitting my head against this for a couple of days, and nothing I can find helps.
I created:
single view application
one button, linked to view controller as an action
a new class, Test_View, subclassing UIView
ViewController code:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var f = Test_View()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func Button_Pressed(sender: AnyObject) {
var f = Test_View()
f.setNeedsDisplay()
NSLog("Button pressed.")
}
}
Test_View code:
class Test_View: UIView {
override func drawRect(rect: CGRect) {
let h = rect.height
let w = rect.width
var color:UIColor = UIColor.yellowColor()
var drect = CGRect(x: (w * 0.25),y: (h * 0.25),width: (w * 0.5),height: (h * 0.5))
var bpath:UIBezierPath = UIBezierPath(rect: drect)
color.set()
bpath.stroke()
NSLog("drawRect has updated the view")
}
}
(Note: the log is updated every time I press the button, so that's not the problem. It's just that the display never changes. Also, I've tried drawing the rectangle with random coordinates, so it's not that it's updating but I'm not seeing it.)
Thanks for any help!