IOS如何从其子女访问uipageviewcontroller?

时间:2015-03-04 22:46:23

标签: ios uipageviewcontroller

我有以下类,第一个包含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。

请帮忙。

1 个答案:

答案 0 :(得分:3)

为孩子添加委托协议:

@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

然后当你创建孩子时:

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