-4

Is it possible to pass data between the UIViewController and the UIView? if yes, how?

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

The buttons are build 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 UnitView: UIView {

    override func drawRect(rect: CGRect) {
        let cirkel = 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(cirkel, 4.0)
        CGContextAddEllipseInRect(cirkel, rectangle)
        CGContextStrokePath(cirkel)
    }
}
A. Steiness
  • 423
  • 1
  • 4
  • 7
  • 7
    yes, that is possible. – luk2302 Dec 29 '16 at 20:29
  • "and then the `UIView` draw a triangle" ... You can have `UIView` draw stuff if you want, but using a `CAShapeLayer` is an easier way to include shapes in a view. – Rob Dec 29 '16 at 20:43
  • The reason for my down vote.... Please research the acronym MVC. Model-View-Controller. It may take just a moment to key this into Google, but after an hour of research on the returns? You'll give yourself a FEW steps on your career. (I'm saying this with full respect of what you already know. But also acknowledging what you seem to not yet. Good luck!) If you understand MVC, you should have already known not just that "passing data" between a controller and a view is what it's about, but also that some things are what it isn't about. –  Dec 29 '16 at 20:50
  • 2
    This question is too broad. Are you asking how to add button? Are you doing it programmatically or via Interface Builder? Are you asking how to link that button to some method (e.g. an `@IBAction`)? Are you asking how to have that action show a triangle or circle? Bottom line, you really should edit this question, narrowing it down to something more specific, clarifying what precisely you're asking, and show us what research you've already done on this topic. Please refer to the help center [about asking questions](http://stackoverflow.com/help/asking) on Stack Overflow. – Rob Dec 29 '16 at 20:55
  • Yeah, you've got to put some elbow grease in doing some studying. I'd say just about _any_ iOS beginners book/reference will show you how to do this. Here some articles on SO that might help you on your way: http://stackoverflow.com/questions/21652019/explanation-wanted-concept-of-passing-data-between-uiviewcontroller-and-uiview, http://stackoverflow.com/questions/34348275/pass-data-between-viewcontroller-and-containerviewcontroller And something on RayWenderlich: https://www.raywenderlich.com/86477/introducing-ios-design-patterns-in-swift-part-1 – Yohst Dec 29 '16 at 22:25
  • Hey guys, i have edited it. – A. Steiness Dec 30 '16 at 14:43

2 Answers2

0

Welcome to SO.

This is a pretty basic beginner's question.

Sure it's possible. You should probably create a custom subclass of UIView. Let's call it ShapeView. Give it methods to draw various shapes, and give it properties to store it's current state.

Now you can add a UIView to your view controller, and go into the "identity inspector" for your view controller scene and change the type of that view to your new ShapeView class. Add an outlet to your shape view in your view controller.

Since your shape view is a custom subclass of UIView with custom methods and properties, your view controller can invoke those methods and set those properties as needed.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
-2

UIViewController has a view property of type UIView.

You can access from inside your UIViewController class using self.view

Accessing it from inside your class:

class MyViewController: UIViewController {
  var buttonLabel:String
  var buttonRadius:CGFloat = 90
  var button = UIButton.buttonWithType(UIButtonType.System) as UIButton

  init(buttonColour:CGColorRef, buttonLabel:String){
    self.buttonColour = buttonColour
    self.buttonLabel = buttonLabel
  }

  func drawButton() {
    button.frame = 100, 100, buttonRadius * 2, buttonRadius * 2)
    button.layer.borderColor = buttonColour
    button.setTitle(buttonLabel, forState: UIControlState.Normal)
    button.addTarget(self, action: "pressed:", forControlEvents: UIControlEvents.TouchUpInside)
    self.view.addSubview(button) 
  }
}

Swift class doesn't like self.view.addSubview()

Community
  • 1
  • 1
Alan
  • 45,915
  • 17
  • 113
  • 134
  • This answer has nothing to do with the question. – rmaddy Dec 29 '16 at 20:37
  • The question is about accessing the `UIView` from inside the `UIViewController.` What did I miss? – Alan Dec 29 '16 at 20:38
  • You missed the question which is about sending a message to a custom view telling it to draw a different shape based on which button was tapped. – rmaddy Dec 29 '16 at 20:39
  • The question is "is it possible to pass data between the UIViewController and the UIView? if yes, how?" – Alan Dec 29 '16 at 20:41
  • That's the 1st line. Read the whole 2nd paragraph. – rmaddy Dec 29 '16 at 20:42
  • So are you saying in order to send messages to the UIView, based on touch events in the UIViewController you don't use the `self.view` property? – Alan Dec 29 '16 at 20:44
  • I'm saying your question makes no attempt to answer the question being asked. – rmaddy Dec 29 '16 at 20:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/131838/discussion-between-alan-and-rmaddy). – Alan Dec 29 '16 at 20:48