gpt4 book ai didi

IOS 如何从其子项访问 uipageviewcontroller?

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:59:00 24 4
gpt4 key购买 nike

我有以下类,第一个包含 UIPageViewController 的实例,第二个是 child 的 View Controller

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

@end

@interface GuidePagerChild : UIViewController <UIWebViewDelegate>

@end

我需要从 GuidePagerChild 所在的 GuidePagerChild 调用 GuidePager 的 flipToPage。

请帮忙。

最佳答案

为 child 添加委托(delegate)协议(protocol):

@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

关于IOS 如何从其子项访问 uipageviewcontroller?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28866603/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com