gpt4 book ai didi

ios - UIPageViewController 以编程方式滑动

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:33:38 25 4
gpt4 key购买 nike

我一直无法找到解决此问题的方法来解决我的问题。我希望调用以编程方式滑动我的页面 View Controller 的两个代码块。

例如 [self viewControllerAfterViewController:self] 将调用最后一个代码块。

viewDidLoad

[super viewDidLoad];
indexvalue = 1;
NSLog(@"main intitated");

// Do any additional setup after loading the view, typically from a nib.
self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageViewController"];
self.pageViewController.dataSource = self;

ViewController *tvc = [self.storyboard instantiateViewControllerWithIdentifier:@"Main"];
Request *pvc;
viewcontrollers =[NSArray arrayWithObjects:tvc, pvc, nil];

[self.pageViewController setViewControllers:viewcontrollers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

[self addChildViewController:pageViewController];
[self.view addSubview:pageViewController.view];

[self.pageViewController didMoveToParentViewController:self];

通过滑动手势调用的在屏幕之间滑动的代码

-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSLog([NSString stringWithFormat:@"%i", indexvalue]);

if (indexvalue == 0 & check0 < 1) {
check0 = 1;
ViewController *tvc = [self.storyboard instantiateViewControllerWithIdentifier:@"Main"];
indexvalue = 1;
return tvc;
}
else {
return nil;
}
}

-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSLog([NSString stringWithFormat:@"%i", indexvalue]);

if (indexvalue == 1 & check1 < 1) {
check1 = 1;
Request *pvc = [self.storyboard instantiateViewControllerWithIdentifier:@"Friends"];
indexvalue = 0 ;
return pvc;
}
else {
return nil;
}
}

最佳答案

viewcontrollers =[NSArray arrayWithObjects:tvc, pvc, nil];

replace with

viewControllers = @[tvc];
viewControllers= [self viewControllerAtIndex:0];

现在添加这个方法

- (UIViewController *)viewControllerAtIndex:(NSUInteger)index
{
if (([self.pageTitles count] == 0) || (index >= [self.pageTitles count])) {
return nil;
}

if (index == 0) {
//first view
ViewController *pageContentViewController2 = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
return pageContentViewController2;

}
//second view

Request *pageContentViewController3 = [self.storyboard instantiateViewControllerWithIdentifier:@"Request"];
return pageContentViewController3;
}

关于ios - UIPageViewController 以编程方式滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36539022/

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