gpt4 book ai didi

ios - 以编程方式更改 UIPageViewController 的页面不会更新 UIPageControl

转载 作者:IT王子 更新时间:2023-10-29 08:18:05 35 4
gpt4 key购买 nike

在我的自定义 UIPageViewController 类中:

- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
self.model = [[BSTCMWelcomingPageViewModel alloc] init];
self.dataSource = self.model;
self.delegate = self;

self.pageControl = [UIPageControl appearance];
self.pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
self.pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
}
return self;
}

然后我在点击按钮时以编程方式设置当前 ViewController:

- (void)scrollToNext
{
UIViewController *current = self.viewControllers[0];
NSInteger currentIndex = [self.model indexForViewController:current];
UIViewController *nextController = [self.model viewControllerForIndex:++currentIndex];
if (nextController) {
NSArray *viewControllers = @[nextController];
// This changes the View Controller, but PageControl doesn't update
[self setViewControllers:viewControllers
direction:UIPageViewControllerNavigationDirectionForward
animated:YES
completion:nil];
//Nothing happens!
[self.pageControl setCurrentPage:currentIndex];

//Error: _installAppearanceSwizzlesForSetter: Not a setter!
[self.pageControl updateCurrentPageDisplay];
}
}

如果我不能用“属于”我的 UIPageViewControllerUIPageControl 来做到这一点,我将尝试自己制作。但如果这是可能的,那就太好了!

最佳答案

要更新您的 UIPageControl 指示器,您需要实现 UIPageViewController 的一种数据源方法(UIPageViewControllerDataSource 方法):

-(NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController

此方法负责更新页面控件指示器(当您使用 UIPageViewController 时)。您只需要在此方法中返回当前页面值。当您在自定义 UIPageViewController 上使用/调用 setViewControllers 时,默认情况下会调用该方法。

所以您需要编写的代码块是:

- (void)scrollToNext
{
UIViewController *current = self.viewControllers[0];
NSInteger currentIndex = [self.model indexForViewController:current];
UIViewController *nextController = [self.model viewControllerForIndex:++currentIndex];
if (nextController) {
NSArray *viewControllers = @[nextController];
// This changes the View Controller and calls the presentationIndexForPageViewController datasource method
[self setViewControllers:viewControllers
direction:UIPageViewControllerNavigationDirectionForward
animated:YES
completion:nil];
}

- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{
return currentIndex;
}

希望这能解决您的问题。 :)

关于ios - 以编程方式更改 UIPageViewController 的页面不会更新 UIPageControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22554877/

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