Search code examples
iosswiftuiimageviewcgcontext

Setting UIImageView to nil


I am attempting to clear a drawing canvas for my simple drawing app. I have created a button to delete the image by seeing the UIImageView.image to nil. However, nothing happens when the button is pressed. The ViewController code:

import UIKit

var smoothLineView : SmoothLineView = SmoothLineView()

class ViewController: UIViewController {

@IBOutlet weak var mainDrawingCanvas: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()

    smoothLineView = SmoothLineView(frame: mainDrawingCanvas.bounds)
    self.view.addSubview(smoothLineView)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func deleteCurrentCanvas(sender: AnyObject) {
    mainDrawingCanvas.image = nil
}

}

The full project can also be found: https://github.com/BananaSplitio/OpenSketch


Solution

  • You add smoothLineView to self.view,then the button won't receive event. Set smoothLineView to blue color then you will understand why it not work.

    You need make a IBOutlet of the button,and add the following code after addsubview smoothLineView,

    self.view.bringSubviewToFront(button)
    

    then you need remove some IBAction of the button you forget to delete. After that,you need change your clear operation in deleteCurrentCanvas method.