您的位置:首页 > 产品设计 > UI/UE

Call Function in a ViewController from UITableViewCell

2015-12-17 08:46 507 查看
http://stackoverflow.com/questions/32068689/call-function-in-a-viewcontroller-from-uitableviewcell

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

Delegation (create
a protocol delegate uitableViewcell) set view controller as delegate of
UITabelVeiwCell
and
then from cell call
self.delegate.whatEverDeelgate()

Post notification from the cell and register observer for that notification inside view controller view
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
Keep a weakReference of viewController inside UITableViewCell, means create a property
@weak
var viewController:YourViewcontroller
and use this to call method on view controller (Not recommended)
Add observer on a key path aka KVO (this
will call a method in view controller when a property changes its value) may be not necessary for your scenario
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: