这是一个关于委托的问题,试图从链接到具有选择器视图的容器视图的子视图控制器传回一些数据。我之前问了一个相关的问题,有人很友好地回答了以下代码:
“我将用一个例子向您解释委派是如何工作的。
按如下方式编辑您的ChildViewController.h:“
@protocol ChildViewControllerDelegate;
@interface ChildViewController : UIViewController
@property (weak)id <ChildViewControllerDelegate> delegate;
@end
@protocol ChildViewControllerDelegate <NSObject >
- (void) passValue:(UIColor *) theValue;
@end在您的ChildViewController.m上,当您想要将某些内容传递回ParentViewController时,请执行以下操作:
- (void) myMethod
{
[delegate passValue:[UIColor redColor]]; // you pass the value here
}在ParentViewController.h上
#import "ChildViewController.h"
@interface ParentViewController : UIViewController <ChildViewControllerDelegate > // you adopt the protocol
{
}在ParentViewController.m上:
- (void) passValue:(UIColor *) theValue
{
//here you receive the color passed from ChildViewController
}现在要小心了。只有在设置代理的情况下,一切才能正常工作。因此,当您在ParentViewController类中声明ChildViewController时,如下所示:
ChildViewController * controller = [[ChildViewController alloc]initWithiNibName:@"ChildViewController"];
controller.delegate = self; //without this line the code won't work!"无论如何,这对我帮助很大,也是我得到的最有用的建议之一。不幸的是,此代码中的一行返回错误,并使委托为空。就是这个:
ChildViewController * controller = [[ChildViewController alloc]initWithiNibName:@"ChildViewController"];错误是:"No visible @interface for 'ChildViewController‘声明了选择器'initWithNibName:’“
当我按照我想要学习的东西的说明去做的时候,我有点迷失了。首先,当我尝试输入它时,所提出的方法接受另一个参数:[ChildViewController allocinitWithNibName:<#(nullable NSString *)#> bundle:<#(nullable NSBundle *)#>];
我执行了一个NSLog来获取self.delegate描述,结果返回为空。有人能帮上忙吗?谢谢
发布于 2016-01-25 08:20:26
您的委托建议听起来不错,但它没有提供通过故事板构建视图控制器的参数。
ChildViewController *controller = [[ChildViewController alloc]
initWithNibName:@"ChildViewController"
bundle:[NSBundle mainBundle]];...will可以工作,但前提是项目中有一个名为"ChildViewController“的接口构建器文件。
https://stackoverflow.com/questions/34982015
复制相似问题