4

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..

PvUIDev
  • 95
  • 1
  • 9
  • 38

3 Answers3

3

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?()
    }

}
King.lbt
  • 843
  • 5
  • 15
1

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)
}
dRAGONAIR
  • 1,181
  • 6
  • 8
0

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
    }
}
EDUsta
  • 1,932
  • 2
  • 21
  • 30
  • Thanks for answering, whether i need to call didClickSaveButton() inside saveBtn action? – PvUIDev May 20 '20 at 12:49
  • Ah, I forgot to add that part. Edited my answer. – EDUsta May 20 '20 at 15:23
  • I have an updated image of what I am trying to do. Here showCardViewController() is not used. how to access save action of webview controller when back btn pressed from the card view controller. can you help me out of this – PvUIDev May 20 '20 at 17:55