Objective c 委托和从childViewController传回数据

Objective c 委托和从childViewController传回数据,objective-c,delegation,childviewcontroller,parentviewcontroller,Objective C,Delegation,Childviewcontroller,Parentviewcontroller,我已经为此挣扎了几天,在路上从S.O.那里得到了宝贵的帮助。我已经做了一个尽可能简单的项目,以减少打字错误的可能性。 我的所有项目都是一个ViewController,它保存一个连接到childViewController的容器视图。“父”ViewController设置为childViewController的代理。在孩子的viewDidLoad中,我传递的值只是一个字符串。此字符串应传递给父级并在控制台上打印。这是文件 ViewController.h #import <UIKit/U

我已经为此挣扎了几天,在路上从S.O.那里得到了宝贵的帮助。我已经做了一个尽可能简单的项目,以减少打字错误的可能性。 我的所有项目都是一个ViewController,它保存一个连接到childViewController的容器视图。“父”ViewController设置为childViewController的代理。在孩子的viewDidLoad中,我传递的值只是一个字符串。此字符串应传递给父级并在控制台上打印。这是文件

ViewController.h

#import <UIKit/UIKit.h>
#import "ChildViewController.h"

@interface ViewController : UIViewController <ChildViewControllerDelegate>

@end
#import <UIKit/UIKit.h>

@protocol ChildViewControllerDelegate;

@interface ChildViewController : UIViewController

@property (weak)id <ChildViewControllerDelegate> delegate;

@end

@protocol ChildViewControllerDelegate <NSObject>

- (void) passValue:(NSString*) theValue;

@end
@property (nonatomic, weak) id <ChildControllerDelegate> delegate;
ChildViewController.h

#import <UIKit/UIKit.h>
#import "ChildViewController.h"

@interface ViewController : UIViewController <ChildViewControllerDelegate>

@end
#import <UIKit/UIKit.h>

@protocol ChildViewControllerDelegate;

@interface ChildViewController : UIViewController

@property (weak)id <ChildViewControllerDelegate> delegate;

@end

@protocol ChildViewControllerDelegate <NSObject>

- (void) passValue:(NSString*) theValue;

@end
@property (nonatomic, weak) id <ChildControllerDelegate> delegate;

我是否正确地认为,当应用程序启动时,控制台应该记录以下消息:“这是我的值:您好”。我是否在逻辑上没有获得授权方面做错了什么,或者这只是一个愚蠢的打字错误?tx

假设视图控制器实例化时加载了视图。这就是它的工作原理。视图在需要时加载(如添加到父视图)

但是您可以强制加载视图并使其工作。设置委托后,立即在子视图控制器上调用
-loadViewIfNeeded
。这可能会让你得到你想要的:

controller.delegate = self;
[controller loadViewIfNeeded];
NSLog(@"Here is my value: %@",self.myValueRetrieved);

或者,如果您确实想在
viewDidLoad
中回调委托,那么您需要将
NSLog
移动到
-passValue:
方法,因为主视图控制器的
viewDidLoad
方法将已完成运行。

要执行此操作,请使ParentController成为ChildController的委托。这允许ChildController将消息发送回ParentController,使我们能够将数据发送回

要使ParentController成为ChildController的代理,它必须符合我们必须指定的ChildController协议。这将告诉ParentController它必须实现哪些方法

在ChildController.h中,在#import下面,但在@interface上面,您指定了协议

@class ChildController;

@protocol ViewControllerBDelegate <NSObject>
- (void)addItemViewController:(ChildController *)controller didFinishEnteringItem:(NSString *)item;
@end
这就是儿童控制器。现在在ParentController.h中,告诉ParentViewController导入子对象并遵守其协议

@class ChildController;

@protocol ViewControllerBDelegate <NSObject>
- (void)addItemViewController:(ChildController *)controller didFinishEnteringItem:(NSString *)item;
@end
导入“ChildController.h” @接口ParentController:UIViewController 在ParentController.m中,从我们的协议实现以下方法

- (void)addItemViewController:(ChildController *)controller didFinishEnteringItem:(NSString *)item
{
    NSLog(@"This was returned from ChildController %@",item);
}
我们需要做的最后一件事是在将ChildController推送到nav堆栈之前告诉ChildController ParentController是它的委托

ChildController *ChildController = [[ChildController alloc] initWithNib:@"ChildController" bundle:nil];
ChildController.delegate = self
[[self navigationController] pushViewController:ChildController animated:YES];

嗨,谢谢你,我遵照你的指示。我对最后一行也有同样的问题。“ChildController*ChildController=[[ChildController alloc]initWithNib:@“ChildController”bundle:nil];”返回错误:“ChildController”没有可见的@接口声明选择器“initWithNib:bundle:”不使用nib初始化。。。。只要试试这个,只有当你有一个nib文件时才使用nib。。。现在我想你正在使用故事板。。。为此,只需使用init
ChildController*ChildController=[[ChildController alloc]init][控制器加载视图如果需要];正在使应用程序崩溃。我不完全理解的是“theValue”是如何接收任何东西的。如果它是一个类似于其他方法的方法,我是否应该在viewDidLoad中调用它,就像这样:[self passValue:]问题是我不想在那里传递参数,我想要这个值。这个问题有意义吗?