我正在尝试使用协议通过segue传回数据。我正在关注这个答案:How to Pass information Back in iOS when reversing a Segue?
所以我已经做了:
PageScrollViewController.h
@protocol MyDataDelegate <NSObject, UIPageViewControllerDelegate>
- (void)recieveData:(NSString *)theData;
@end
并且我还添加了该属性:
@property (nonatomic,weak) id<MyDataDelegate> delegate;
ContainerViewController.h
#import "PageScrollViewController.h"
@interface ContainerViewController : UIViewController <MyDataDelegate>
//...
ContainerViewController.m
- (void)recieveData:(NSString *)theData {
//Do something with data here
}
在这里我被卡住了:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
//Get a handle on the view controller about be presented
PageScrollViewController *secondViewController = segue.destinationViewController;
if ([PageScrollViewController isKindOfClass:[PageScrollViewController class]]) {
PageScrollViewController.delegate = self;
}
}
PageScrollViewController.delegate = self;
行给出了一个错误:在类型'PageScrollViewController'的对象上找不到属性'delegate',但是我确实将其添加为属性并对其进行了合成... 最佳答案
您正在尝试对 [PageScrollViewController setDelegate:]
类型的对象(在本例中为Class
)调用实例方法(PageScrollViewController
)。
尝试以下操作:
更换
PageScrollViewController *secondViewController = segue.destinationViewController;
if ([PageScrollViewController isKindOfClass:[PageScrollViewController class]]) {
PageScrollViewController.delegate = self;
}
用
PageScrollViewController *secondViewController = segue.destinationViewController;
if ([secondViewController isKindOfClass:[PageScrollViewController class]]) {
secondViewController.delegate = self;
}