PvUIDev
PvUIDev

Reputation: 125

How to call UIButton action from view controller to another view controller in iOS Swift?

In Webview Controller, I have UIButton action to perform save functionality. I can able to perform actions inside the web view controller. In my case, want to perform a save action of a webview controller from cardViewController. But nothing performs.

Here is code for CardViewController:

  @IBAction func backBtn(_ sender: Any) {


        WebViewController().saveBtn(self)

   }

Here is code for the webview controller:

    @IBAction func saveBtn(_ sender: Any) {

    // handling code
    print("save button tapped")
    tapCallback?()


  }

enter image description here

any help much appreciated pls..

Upvotes: 4

Views: 1561

Answers (3)

dRAGONAIR
dRAGONAIR

Reputation: 1191

Since, in this case, both CardViewController and WebViewController are presented simultaneously, you can take the 'Notifications' approach. post a notification from CardViewController when the user clicks backButton. This notification is observed in WebViewController which is linked to saveBtn function.

//CardViewController
@IBAction func backBtn(_ sender: Any) {
    NotificationCenter.default.post(name: NSNotification.Name(rawValue:"backButtonPressed"), object: nil)
}

This notification can be observed in WebViewController

//WebViewController
override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(self. saveBtn), name: "backButtonPressed", object: nil)
}

func saveBtn() {
    // your code
}

deinit {
    NotificationCenter.default.removeObserver(self)
}

Upvotes: 1

King.lbt
King.lbt

Reputation: 881

In your CardViewController

let webViewController = WebViewController() //Init WebViewController

webViewController.onSave = { [weak self] in
    // Do your logic when save done in WebViewController
}

present(webViewController, animated: true, completion: nil)

In your WebViewController :

class WebViewController: UIViewController {

    var onSave:(()->())?

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

    @IBAction func buttonSaveInWebViewController(_ sender: Any) {
     // Do your logic
         onSave?()
    }

}

Upvotes: 3

EDUsta
EDUsta

Reputation: 1933

You're creating a brand new WebViewController, but you should have the reference for your actual WebViewController. You could create a delegation connection like:

protocol CardViewControllerDelegate: AnyObject {
    func didClickSaveButton()
}

Then make it settable in CardViewController:

class CardViewController {
    ....
    weak var delegate: CardViewControllerDelegate?
    ....
}

and let WebViewController implement that:

extension WebViewController: CardViewControllerDelegate {
    func didClickSaveButton() {
        // handling code
        print("save button tapped")
        tapCallback?()
    }

Then, set the delegate when you create CardViewController in WebViewController:

func showCardViewController() { // or whatever the name is
    let cardVC = CardViewController() // or however you load it
    cardVC.delegate = self
    present(cardVC, animated: true) // or however you show it
}

Lastly, connect to the delegate:

@IBAction func backBtn(_ sender: Any) {
    delegate?.didClickSaveButton()
}

If the view controllers are made in Storyboard, and there's a segue between them, you could set the delegate in prepareForSegue, like:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let cardViewController = segue.destination as? CardViewController {
        cardViewController.delegate = self
    }
}

Upvotes: 0

Related Questions