Question about delegate and protocol in swift (with example)

Jackey Yiu

I am trying to work with delegate and protocol but met a problem.

I created 2 buttons on one ViewController and created 1 imageView in another ShowViewController. The color of the imageView will change according to which button is pressed.

ViewController.swift

import UIKit

protocol getColorProtocol {
    func getColor(color:String?)
}


class ViewController: UIViewController {

    var color: String?

    var delegate:getColorProtocol?

    @IBAction func blueButtonPressed(_ sender: Any) {

        color = "blue"
        delegate?.getColor(color: color)
    }

    @IBAction func redButtonPressed(_ sender: Any) {

        color = "red"
        delegate?.getColor(color: color)

    }


    override func viewDidLoad() {
        super.viewDidLoad()

    }



}

ShowViewController.swift

import UIKit


class ShowViewController: UIViewController, getColorProtocol {

    var viewOne = ViewController()

    @IBOutlet weak var colorView: UIImageView!

    func getColor(color: String?) {
        print("color is \(color!)")

        if color == "red"{
            colorView.backgroundColor = UIColor(displayP3Red: 255/255, green: 0/255, blue: 0/255, alpha: 1)
        }
        else if color == "blue" {
            colorView.backgroundColor = UIColor(displayP3Red: 0/255, green: 0/255, blue: 255/255, alpha: 1)

        }


    }

    override func viewDidLoad() {
        super.viewDidLoad()

        viewOne.delegate = self

    }

}

press here to view the storyboard!

You may check my storyboard above.

Currently, it has no error but it cannot call the getColor function. I am wondering if the problem is coming from the delegate?.getColor(color: color) statement.

Any ideas?

Tung Vu Duc

First of all, in your case, you don't need to use delegate pattern

Your ViewController connect directly to ShowViewController through segue You can use prepareForSegue instead

Secondly, I don't think you understand how delegate pattern works, Inside ShowViewController, you're create a new ViewController called viewOne, it isn't the original ViewController so that's why your code doesn't work as expected

As I mentioned above, you should use segue

first, go to the storyboard setup your segue identifier. Then, inside your ViewController

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "goToShowVC") {
    //your code here
}
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Question about Delegate / Protocol requirements

Question about encapsulation with example question

Swift protocol delegate returning nil

Protocol Delegate Does not Trigger Swift

Swift 2 - protocol delegate not called

Swift Error on communication Protocol/Delegate

Swift 3 Protocol and Delegate Method?

Protocol Delegate returns nil swift

Question About An Time Complexity Example

Simple Question about Delegate and View Controllers

Confusions about weak delegate in swift

Protocol Delegate not working as expected returns nil swift

swift protocol delegate always return nil

iOS Swift - Protocol sending delegate to wrong TableViewController

Swift - UIView Delegate Protocol not returning value

Swift protocol method is not being called by the delegate

Implement delegate using generic protocol in Swift

Trying to learn about encapsulation example question included

Question about randomstate usage in an svc example

Protocol Oriented Programming Example not working in swift 3.0

Swift Protocol and Delegate movement Back through multiple classes

Why is my simple swift delegate and protocol setup not working?

Swift protocol extension with method in wrong delegate still works(?!)

Swift: XCTest delegate/protocol call backs (Unit test)

Access delegate protocol from Swift framework into Objective C

iOS - Pass Data from Swift to ObjC VC using protocol and delegate

@obj protocol delegate method not working in second class. Swift

Swift - Protocol Delegate from TableView not working - Present Modally

How to edit and pass back cell data with delegate and protocol in swift