ishtyaq haider jafri
ishtyaq haider jafri

Reputation: 43

transferring Action from 1 view controller to another

is the MAIN VIEW containing the Containeri have a button(procceed) on the containerView of a viewcontroller."procceed" on being tapped needs to show a color change of another button placed on the main view of which the above former is a part.

is the CONTAINER view having the button procceed

Upvotes: 0

Views: 81

Answers (2)

Siba Prasad Hota
Siba Prasad Hota

Reputation: 4789

You can use Delage method like below.

import UIKit

protocol containerVCDelegate: class {
       func changeBackgroundColor(_ color: UIColor?)
}

class containerVC: UIViewController {

    weak var delegate: containerVCDelegate?

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func proceedClicked(_ sender: UIButton) {
        delegate?.changeBackgroundColor(backgroundColor)
    }

}

Below codes should be there in your Main Vc

import UIKit


class mainVC: UIViewController, containerVCDelegate {


    override func viewDidLoad() {
        super.viewDidLoad()
        containerVc.delegate = self
    }


    func changeBackgroundColor(_ color: UIColor?) {
        view.backgroundColor = color
    }

}

you can get the complete project here: https://github.com/jamesrochabrun/DelegateTutorialFinal Refer this link for more clarification on this concept written by James Rochabrun.

Upvotes: 0

marc
marc

Reputation: 914

I would suggest using NotificationCenter like so:

ContainerViewController.swift

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "updateButtonColor"), object: nil, userInfo: nil)

MainViewController.swift

NotificationCenter.default.addObserver(self, selector: #selector(self.updateColor), name: NSNotification.Name(rawValue: "updateButtonColor"), object: nil)


@objc func updateColor() {

    CartButton.tintColor = .green

}

Upvotes: 1

Related Questions