Reputation: 43
i 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.
Upvotes: 0
Views: 81
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
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