我已经为此苦苦挣扎了几天,并在途中得到了宝贵的帮助,所以我做了一个最简单的项目,以减少它成为错字的可能性。我的所有项目都是一个 ViewController,它包含一个连接到 childViewController 的容器视图。“父” ViewController 被设置为 childViewController 的委托。在孩子的 viewDidLoad 中,我传递的值只是一个字符串。此字符串应传递给父级并打印在控制台上。这是文件。
视图控制器.h
#import <UIKit/UIKit.h>
#import "ChildViewController.h"
@interface ViewController : UIViewController <ChildViewControllerDelegate>
@end
视图控制器.m
#import "ViewController.h"
@interface ViewController ()
@property NSString *myValueRetrieved;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
ChildViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"ChildVC"];
controller.delegate = self;
NSLog(@"Here is my value: %@",self.myValueRetrieved);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void) passValue:(NSString *)theValue{
self.myValueRetrieved = theValue;
}
@end
ChildViewController.h
#import <UIKit/UIKit.h>
@protocol ChildViewControllerDelegate;
@interface ChildViewController : UIViewController
@property (weak)id <ChildViewControllerDelegate> delegate;
@end
@protocol ChildViewControllerDelegate <NSObject>
- (void) passValue:(NSString*) theValue;
@end
ChildViewController.m
#import "ChildViewController.h"
@interface ChildViewController ()
@property NSArray *colors;
@end
@implementation ChildViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.delegate passValue:@"Hello"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
我是否认为当应用程序启动时,控制台应该记录以下消息:“这是我的值:你好”。我在逻辑上没有获得授权方面做错了什么,还是只是某个地方的愚蠢错字?发送