Home > Mobile >  Swift, Get action Bar Button Action from Container
Swift, Get action Bar Button Action from Container

Time:05-14

I have ParentViewController.swift and ChildContainer.swift. In ParentViewController, I have bar button item action like below :

@IBAction func onClickBarItemRefresh(_ sender: UIBarButtonItem) {
    print("Refresh")
}

I want to know, how to call/get this action from ChildContainer? I can change the title with parent?.navigationItem.title = "YourName", but I cannot find related question about to get the action.

Addition Info: I have like 4 or 5 container in 1 ParentViewController, so all logic is on their container. So I need call the action on 4 or 5 child container with different login inside the action

CodePudding user response:

First declare a callback function in the ContainerViewController.

var refreshButtonTapped: (() -> Void)?

In ParentViewController where you initialise ContainerViewController give action of the callback function.

In your case create a separate method i.e. refreshContent() and call it from onClickBarItemRefresh() method and also in refreshButtonTapped function where you initialise ContainerViewController.

class ParentViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .white
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .refresh, target: self, action: #selector(onClickBarItemRefresh(_:)))
        
        showContainerVC()
    }
    
    @objc func onClickBarItemRefresh(_ sender: UIBarButtonItem) {
        refreshContent()
    }
    
    private func refreshContent() {
        print("Refresh Content")
    }
    
    func showContainerVC() {
        let vc = ContainerViewController()
        // call refreshContent() inside the callback function
        vc.refreshButtonTapped = { [weak self] in
            self?.refreshContent()
        }
        let nav = UINavigationController(rootViewController: vc)
        self.addChild(nav)
        nav.view.frame = CGRect(x: 20, y: 100, width: 320, height: 200)
        self.view.addSubview(nav.view)
        nav.didMove(toParent: self)
    }
}

In ContainerViewController where you want to perform the action of refreshContent() just call the callback function refreshButtonTapped like below. For example i call it from viewDidAppear() method. It will perform the action of refreshing in ParentViewController.

class ContainerViewController: UIViewController {
    
    var refreshButtonTapped: (() -> Void)?
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .yellow
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        print(#function)
        // call this where you want to perform refreshing of ParentViewController
        refreshButtonTapped?()
    }
}
  • Related