[英]Call Function in a ViewController from UITableViewCell
我如何可以调用的函数ViewController
从一个自定义的tableview细胞UITableView
在ViewController
(使用SWIFT)?
There are few ways you can do that 你可以用很少的方法做到这一点
UITabelVeiwCell
and then from cell call self.delegate.whatEverDeelgate()
UITabelVeiwCell
委托,然后从单元调用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 NSNotificationCenter.defaultCenter().addObserver(self, selector: "nameOfSelector", name: "Name Of Notification", object: nil)
然后从tableView单元格postNotification,但请确保您也想删除Observer,请访问此链接以获取更多详细信息 @weak var viewController:YourViewcontroller
and use this to call method on view controller (Not recommended) @weak var viewController:YourViewcontroller
并使用它来调用视图控制器上的方法(不推荐) Post a notification from your cell: 发布您单元格的通知:
NSNotificationCenter.defaultCenter().postNotificationName("notificationName", object: nil)
Then listen out for it in your viewController: 然后在viewController中监听它:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "functionToCall", name: "notificationName", object: nil)
Make sure you define the new function in your viewController: 确保在viewController中定义新函数:
func functionToCall() {
//This function will be called when you post the notification
}
You can declare a protocol
in your UIableViewCell
class. 您可以在
UIableViewCell
类中声明protocol
。
Make your ViewController
delegate of this protocol. 使您的
ViewController
委托该协议。
On some action from your cell, call this delegate method. 在您的单元格中执行某些操作时,请调用此委托方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.