作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
@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/
我是一名优秀的程序员,十分优秀!