Posts

Post marked as solved
2 Replies
2.5k Views
I'm using MVC pattern and playing around with protocol and delegate to understander how it works. After reading about it. It seems straight forward. But I'm not getting the result that is expected. I have searched and found a lot explaining what I have previously read, but no prevail on helping me. I feel like it's something simple and I'm not seeing it. So, Looking for some help.In my View folder, I have a UIView called MyUIView. This where I created a delegate protocol, added delegate property and called the delegate method.protocol didTapButton { func printMyText(takeIn stringValue: String) } class MyView: UIView { @IBOutlet weak var btnMyButton: UIButton! @IBOutlet weak var txtMyTextField: UITextField! var myDelegate: didTapButton? @IBAction func MyButton(_ sender: Any) { print("Button is Tapped") self.myDelegate?.printMyText(takeIn: txtMyTextField.text!) } }In my Controller folder, I have a UIViewController called ViewController. This is where I adopt and implement the delegate protocol.class ViewController: UIViewController, didTapButton { func printMyText(takeIn stringValue: String) { print("String value result: \(stringValue)") } override func viewDidLoad() { super.viewDidLoad() let delegate = MyView() //instance on MyView delegate.myDelegate = self } }
Posted
by EDDY39.
Last updated
.