Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS How to access uipageviewcontroller from its children?

I have the below classes, the first contains an instance of UIPageViewController and the second is the view controller of the childs

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

@end

AND

@interface GuidePagerChild : UIViewController <UIWebViewDelegate>

@end

I need to call flipToPage of the GuidePager from GuidePagerChild where GuidePagerChild.

Please help.

like image 320
yjradeh Avatar asked Dec 29 '25 18:12

yjradeh


1 Answers

Add a delegate protocol for the 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

Then:

@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

Then when you create the 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.

Then:

@implementation GuidePager

...

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

...

@end
like image 63
i_am_jorf Avatar answered Dec 31 '25 10:12

i_am_jorf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!