问题描述
如何在目标C中实现原始回调?
How does one implement a raw callback in objective C?
我只想在我的UITableViewCell对象(自定义单元格)之一检测到触摸时通知ViewController.
I just want to notify a ViewController when one of my UITableViewCell objects(custom cell) detects touch.
我需要通知我的RootViewController,以便它可以创建另一个ViewController的对象并将其推入导航堆栈中.
I need to notify my RootViewController so that it can create an object of another ViewController and push it on the navigation stack.
推荐答案
您可以对secondVC使用如下所示的自定义init方法,并将_sender存储在全局变量或类变量中.像
You can use a custom init method like below for secondVC and store the _sender in global or class variable. like
id sender;
- (id)initWithSender:(id)_sender
{
self = [super init];
if (self) {
sender=_sender;
}
return self;
}
从RootVC中
如下初始化secondvc并定义一个名为-(void)touchDetected的方法;在rootvc中.
from RootVC initialize secondvc as follows and define a method named -(void) touchDetected; in rootvc.
secondvc=[[SecondVC alloc] initWithSender:self];
[[self navigationController] pushViewController: secondvc animated:YES];
在secondvc调用中检测到触摸时,这将通知rootvc在secondvc中检测到触摸.
when the touch is detected in secondvc call, this will notify your rootvc that the touch is detected in secondvc.
[sender touchDetected];
这篇关于在目标C中实现基本的回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!