IOS 如何从其 children 访问 uipageviewcontroller?

IOS How to access uipageviewcontroller from its children?

我有下面的 类,第一个包含 UIPageViewController 的实例,第二个是孩子的视图控制器

@interface GuidePager : UIViewController     <UIPageViewControllerDataSource, UIPageViewControllerDelegate>
@property (strong, nonatomic) UIPageViewController *pageController;
@property NSArray *viewControllers;
- (void)flipToPage:(NSString *)index;

@end

@interface GuidePagerChild : UIViewController <UIWebViewDelegate>

@end

我需要从 GuidePagerChild 调用 GuidePager 的 flipToPage,其中 GuidePagerChild.

请帮忙。

为 child 添加委托协议:

@protocol GuidePagerChildDelegate;

@interface GuidePagerChild : UIViewController <UIWebViewDelegate>
@property (nonatomic, weak) id<GuidePagerChildDelegate> delegate;   
@end

@protocol GuidePagerChildDelegate <NSObject>
@required
- (void)guidePagerChild:(GuidePagerChild *)child flipToPage:(NSString *)index
@end

然后:

@interface GuidePager : UIViewController <
    GuidePagerChildDelegate,  // Add the new protocol to the list.
    UIPageViewControllerDataSource, 
    UIPageViewControllerDelegate
>
@property (strong, nonatomic) UIPageViewController *pageController;
@property NSArray *viewControllers;
- (void)flipToPage:(NSString *)index;

@end

然后当您创建 child:

GuidePagerChildDelegate *child = [[GuidePagerChild alloc] init];  // Or however you create it.
child.delegate = self;  // Assuming you create it from within GuidePager.  If not, get a pointer to GuidePager and set it as the delegate.

然后:

@implementation GuidePager

...

- (void)guidePagerChild:(GuidePagerChild *)child flipToPage:(NSString *)index 
{
     [self flipToPage:index];
}

...

@end