简体   繁体   中英

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


  }

在此处输入图像描述

any help much appreciated pls..

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

}

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM