Marmelador
Marmelador

Reputation: 1017

Changing a Views textLabel from a different View

I have a custom class called colorChangerViewClass that inherits from UIView:

class colorChangerViewClass: UIView {


@IBOutlet weak var controller: ViewController!

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    // I want to change a textLabel in the main ViewController based on the current finger position here!
    }

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    // And here aswell!
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { ... }


func setColor(color: UIColor) {
    self.backgroundColor = color
}

}

Inside the touchesBegan and touchesMoved methods I want to change a textLabel in the main ViewController (a different one) based on the current finger position.

What is the best way to establish communication between the two classes colorChangerViewClass and ViewController?

Upvotes: 1

Views: 78

Answers (2)

dip
dip

Reputation: 3598

In your case you are trying to communicate child to parent, there are several ways from which you can gain desired out put.

1.Delegation

2. Notification

3. Passing Parent instance object to child (similar as delegate)

I am demonstrating with delegate, you can use delegation as:-

1. Declare Declare a protocol somewhere out side your ColorChangerViewClass

protocol ColorChangerViewClassDelegate:class {
    func fingerDidMoved()
    }

2. Create delegate var inside your ColorChangerViewClass

class ColorChangerViewClass: UIView {

        //declare delegate var

        weak var delegate:ColorChangerViewClassDelegate?

        @IBOutlet weak var controller: ViewController!

        override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
            // I want to change a textLabel in the main ViewController based on the current finger position here!
            self.notify()

        }

        override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

            self.notify()
            // And here aswell!
        }

        override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {


        }

        func setColor(color: UIColor) {
            self.backgroundColor = color
        }

        func notify() {
            if let delegate = self.delegate {
                delegate.fingerDidMoved()
            }
        }

        }

3. Set Delegate of your view to the controller

class SomeVC:UIViewController,ColorChangerViewClassDelegate {

    var myLabel = UILabel()
        override func viewDidLoad() {
            super.viewDidLoad()

            //your view
            let theView = ColorChangerViewClass() // you might need auto layout or frame declared on your view to define view size
            theView.delegate = self
        }

        //MARK:-deleate method of view
        func fingerDidMoved() {
           // set text from here 
           self.myLabel.text = "Your text"
        }
    }

Upvotes: 1

Nirav D
Nirav D

Reputation: 72460

You are too close to solve the problem, you need to set the reference of your ViewController to controller property of your colorChangerViewClass. Also no need to declare controller as outlet so change its declaration like below.

var controller: ViewController?

Now in touchesBegan and touchesMoved use this controller to access textLabel

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.controller?.textLabel.text = //set text
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.controller?.textLabel.text = //set text
}

Now in your ViewController class where you are creating object of colorChangerViewClass set the controller property of it.

@IOutlet var colorView: colorChangerViewClass!

//set controller property in viewDidLoad
colorView.controller = self

Note: class name always start with Uppercase letters so it would be better if you changed your class name to ColorChangerViewClass from colorChangerViewClass.

Upvotes: 1

Related Questions