0

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

Leo
  • 24,596
  • 11
  • 71
  • 92
Andrew Walz
  • 890
  • 2
  • 8
  • 27
  • http://stackoverflow.com/questions/8979585/reload-refresh-subviews-setneedsdisplay-doesnt-work – Adrian Jul 08 '15 at 01:37

2 Answers2

2

Not exactly sure of what you are trying to do here. But, here is what I think is happening.

You seem to be using just the bounds of the image (mainDrawingCanvas) to make another view (smoothlineView). No matter what you do with the image afterwards it doesn't matter because the image is not part of you view. Your view is smoothlineView, not mainDrawingCanvas.

To remove/delete the image (which I assume is smoothlineView), try doing this, it should work.

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) {
        smoothLineView.removeFromSuperview()
    }

}
ndmeiri
  • 4,979
  • 12
  • 37
  • 45
  • I tried your suggestion, but the result was the same. I did what NSDeveloper suggested and change the background colour to blue. The button disappears. I could only see it because the background was clear. It is being drawn over – Andrew Walz Jul 08 '15 at 02:38
0

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.

NSDeveloper
  • 1,630
  • 15
  • 25
  • That makes a lot of sense! With that, I can actually click the button and changing to removeFromSuperview() does delete the canvas. However, it doesn't let you draw again as the subview has been removed – Andrew Walz Jul 08 '15 at 02:55
  • You need provide a clear method for smoothLineView not remove it. – NSDeveloper Jul 08 '15 at 02:57