我如何可以调用的函数ViewController
从一个自定义的tableview细胞UITableView
在ViewController
(使用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
}
You can declare a protocol
in your UIableViewCell
class.
Make your ViewController
delegate of this protocol.
On some action from your cell, call this delegate method.
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.