How can I call a function of a ViewController
from a custom tableview cell of a UITableView
in that ViewController
(using SWIFT)?
There are few ways you can do that
UITabelVeiwCell
and then from cell call self.delegate.whatEverDeelgate()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "nameOfSelector", name: "Name Of Notification", object: nil)
and then from tableView cell postNotification, but make sure you want to removeObserver as well, visit this link for more detail@weak var viewController:YourViewcontroller
and use this to call method on view controller (Not recommended)Post a notification from your cell:
NSNotificationCenter.defaultCenter().postNotificationName("notificationName", object: nil)
Then listen out for it in your viewController:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "functionToCall", name: "notificationName", object: nil)
Make sure you define the new function in your viewController:
func functionToCall() {
//This function will be called when you post the notification
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With