简体   繁体   中英

How do i tell my UIView that the button in UIViewController has been pressed? (Swift)

Is it possible to pass data between the UIViewController and the UIView ? if yes, how? I can not find anything on the internet which explains it.

For example when you press button1 the UIViewController says to the UIView that the person has pressed button1 and then the UIView draws a Circle. But if you then press button2 the UIViewController says to the UIView that he has now pressed button2 and then the UIView eliminates the Circle and draws a triangle.

The buttons are built programmatically.

The question is how I can invoke the shapes which I have drawn with drawRect within the UIView in the UIViewController .

class ViewController: UIViewController { 
   @IBOutlet var UnitViewTabeller: UnitView!
   var btn: UIButton!

   override viewDidLoad {
     self.btn = UIButton(frame: CGRectMake(0.04 * view.bounds.width, 0.91 * view.bounds.height, 0.44 * view.bounds.width, 0.07 * view.bounds.height))  //set frame
            self.btn.layer.cornerRadius = 10
            self.btn.setTitle("0", forState: .Normal)  //set button title
            self.btn.setTitleColor(UIColor.whiteColor(), forState: .Normal) //set button title color
            self.btn.backgroundColor = UIColor(red: 0.2745, green: 0.2784, blue: 0.2706, alpha: 1.0) //set button background color
            self.btn.tag = 0 // set button tag
            self.btn.addTarget(self, action: "btnclicked:", forControlEvents: .TouchUpInside) //add button action
            self.view.addSubview(btn) //add button in view
            self.btnArray.append(btw)
   }

   func btnclicked(sender: UIButton) {
       //draw the Circle from the UIView
   }


}    
    class CircleView: UIView {

    override func drawRect(rect: CGRect) {
        let Circle = UIGraphicsGetCurrentContext()
        let rectangle = CGRect(x: 0.2 * bounds.width, y: 0.15 * bounds.height, width: 0.6 * bounds.width, height: 0.6 * bounds.width)
        CGContextSetLineWidth(Circle, 4.0)
        CGContextAddEllipseInRect(Circle, rectangle)
        CGContextStrokePath(Circle)
    }
}

Here is an example in Swift 3 of a ShapeView that redraws itself when its shape property is set:

class ShapeView: UIView {
    var shape: Shape = .blank {
        didSet {
            // calling setNeedsDisplay() triggers a redraw of ShapeView
            setNeedsDisplay()
        }
    }

    enum Shape {
        case circle
        case triangle
        case blank
    }

    override func draw(_ rect: CGRect) {
        switch shape {
        case .circle: drawCircle()
        case .triangle: drawTriangle()
        default: break
        }
    }

    func drawCircle() {
        let circle = UIGraphicsGetCurrentContext()
        let rectangle = CGRect(x: 0.2 * bounds.width, y: 0.15 * bounds.height, width: 0.6 * bounds.width, height: 0.6 * bounds.width)
        circle?.setLineWidth(4.0)
        circle!.addEllipse(in: rectangle)
        circle!.strokePath()
    }

    func drawTriangle() {
        // code to draw a triangle
    }
}

Assuming you have a ShapeView object called shapeView as a property of your ViewController , you'd call it like this:

func drawCircleButton(_ sender: UIButton) {
    shapeView.shape = .circle
}

func drawTriangleButton(_ sender: UIButton) {
    shapeView.shape = .triangle
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM