Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass data from containerView to MainViewControler

Tags:

ios

swift

I have this code:

MainViewControler:
class MainViewControler: UIViewController, ContainerToMaster {
    @IBOutlet weak var systemContainerView: UIView!
    @IBOutlet weak var favoriteProductBtn2: UIButton!
    weak var containerViewController: Calculator1ViewController?

    func changeBtn() {
        favoriteProductBtn2.isHidden = true
        print("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
    }
}


CallculatorViewController:
@IBOutlet weak var containerView: UIView!

class CallculatorViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}


Calculator1ViewController:
protocol ContainerToMaster {
    func changeBtn()
}

class Calculator1ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        containerToMaster?.changeBtn()
    }
}

I have such an application layout:

  1. Main view = MainViewControler. In MainViewControler, I have a containerView (systemContainerView) in which the MainViewControler is located

  2. In the CallculatorViewController, I have the next containerView in which the Calculator1ViewController is located.

When entering the Calculator1ViewController the function: containerToMaster?.changeBtn() should start (it should work in MainViewControler).

The problem is - this function does not work :(

Does anyone know how to fix it?

like image 996
traff Avatar asked Dec 06 '25 05:12

traff


1 Answers

A contained viewController is embedded with an embed segue. You need to override prepare(for:sender) and set self as the containerToMaster delegate in the destination viewController. Your situation is complicated by the fact that you have a container view embedded in another container view, so you'll need to set up two delegates and pass the button call back:

protocol ContainerToMaster {
    func changeBtn()
}


class MainViewControler: UIViewController, ContainerToMaster {
    @IBOutlet weak var systemContainerView: UIView!
    @IBOutlet weak var favoriteProductBtn2: UIButton!
    weak var containerViewController: Calculator1ViewController?

    func changeBtn() {
        favoriteProductBtn2.isHidden = true
        print("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let destVC = segue.destination as? CallculatorViewController {
            destVC.containerToMaster = self
        }
    }
}

class CallculatorViewController: UIViewController, ContainerToMaster {
    weak var containerToMaster: ContainerToMaster?

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

    func changeBtn() {
        // pass the button call back
        containerToMaster?.changeBtn()
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let destVC = segue.destination as? Calculator1ViewController {
            destVC.containerToMaster = self
        }
    }
}

class Calculator1ViewController: UIViewController {
    weak var containerToMaster: ContainerToMaster?

    override func viewDidLoad() {
        super.viewDidLoad()
        containerToMaster?.changeBtn()
    }
}
like image 93
vacawama Avatar answered Dec 09 '25 02:12

vacawama



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!