![](/img/trans.png)
[英]How to use delegate methods to pass objects between two view controllers?
[英]How to implement custom delegate methods for my view controllers
在我的应用程序中有两个标签栏。 在 tab-0 父 class 是“FirstViewController”,在 tab-1 父 class 是“SecondViewController”。 在“SecondViewController”中,我声明了协议和自定义委托方法。 我想在“FirstViewController”(FVC)中传递信息。 所以 FVC 必须被指定为代表。
现在我的疑问是,现在我在“SVC”中。 如何将“FVC”指定为“SVC”的代表?
在“SVC”中
[[self delegate] sendCoordinates:self];
方法的定义在“FVC”中。 要执行此方法,首先我需要将“FVC”指定为委托。
我希望我能清楚地解释我的问题。
提前致谢。
您需要设置委托。 让我演示一下:
`SVC.h`
@interface SVC
{
id _delegate;
}
- (void)setDelegate:(id)delegate; //sets the delegate
- (id)delegate; //gets the delegate
@end
@protocol SVCDelegate
- (void)sendCoordinates:(SVC *)svc;
@end
然后,在SVC.m
中,您调用委托的方式与您在问题中显示的方式完全相同,因此[[self delegate] sendCoordinates:self];
现在,在FVC
中,您需要#import SVC.h
并初始化 object。
SVC*svcObject = [[SVC alloc] init];
[svcObject setDelegate:self]; //self now refers to FVC - I think you're missing this one
//work with the object and when you're done, get rid of it in dealloc:
[svcObject setDelegate:nil];
[svcObject release];
在同一个 class 中,实现- (void)sendCoordinates:(SVC *)svc
,它会在您设置委托后调用。
我认为您缺少setDelegate:
阶段,这就是它不起作用的原因。
希望能帮助到你!
注意:在 SVC 中,请记住保留委托,否则它将变为nil
,并且永远不会调用任何委托方法。 完成后不要忘记释放该委托。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.