UIPageViewController - как я могу перейти к определенному номеру страницы?
Я провел почти 8 часов, узнав, как перейти к определенному номеру страницы в UIPageViewController... ниже мой проект выглядит как
Я хочу сделать приложение, которое выглядит как Ibooks ---
Я воспользовался приведенным здесь кодом - http://www.ioslearner.com/tag/uipageviewcontroller-sample-code/
Я сделал plist и UITableView, я выбираю значение из TableView и отображаю то же самое в webView, размещенном в UIPAgeiewController, но проблема в том, что изменяется только страница в веб-представлении, а не фактический номер страницы UIPageViewController...
Если я сужу свой вопрос, это будет выглядеть так: есть ли способ переключения между номерами страниц в UIPageViewController....???
Ответы
Ответ 1
Вам необходимо создать экземпляр контроллера (ов) представления, который управляет страницами, на которые вы хотите перейти, и затем вызвать метод setViewControllers:direction:animated:completion:
управления просмотром страницы, передав новый контроллер представления.
Ответ 2
Вот еще один пример перехода на страницу с анализируемым индексом:
-(void)gotoPage:(int)index{
SinglePageViewController *viewController = [self viewControllerAtIndex:index];
UIPageViewControllerNavigationDirection direction;
if(_curIndex <= index){
direction = UIPageViewControllerNavigationDirectionForward;
}
else
{
direction = UIPageViewControllerNavigationDirectionReverse;
}
if(_curIndex < index)
{
for (int i = 0; i <= index; i++)
{
if (i == index) {
[self.pageViewController setViewControllers:@[viewController]
direction:direction
animated:YES
completion:nil];
}
else
{
[self.pageViewController setViewControllers:@[[self viewControllerAtIndex:i]]
direction:direction
animated:NO
completion:nil];
}
}
}
else
{
for (int i = _curIndex; i >= index; i--)
{
if (i == index) {
[self.pageViewController setViewControllers:@[viewController]
direction:direction
animated:YES
completion:nil];
}
else
{
[self.pageViewController setViewControllers:@[[self viewControllerAtIndex:i]]
direction:direction
animated:NO
completion:nil];
}
}
}
_curIndex = index;
}
Ответ 3
Я использую эту функцию (я всегда в ландшафтном, 2-страничном режиме)
-(void) flipToPage:(NSString * )index {
int x = [index intValue];
LeafletPageContentViewController *theCurrentViewController = [self.pageViewController.viewControllers objectAtIndex:0];
NSUInteger retreivedIndex = [self indexOfViewController:theCurrentViewController];
LeafletPageContentViewController *firstViewController = [self viewControllerAtIndex:x];
LeafletPageContentViewController *secondViewController = [self viewControllerAtIndex:x+1 ];
NSArray *viewControllers = nil;
viewControllers = [NSArray arrayWithObjects:firstViewController, secondViewController, nil];
if (retreivedIndex < x){
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL];
} else {
if (retreivedIndex > x ){
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:NULL];
}
}
}
Возможно, вам это тоже нужно
- (LeafletPageContentViewController *)viewControllerAtIndex:(NSUInteger)index {
if (([self.modelArray count] == 0) || (index >= [self.modelArray count])) {
return nil;
}
LeafletPageContentViewController *dataViewController;
dataViewController = [[LeafletPageContentViewController alloc]initWithNibName:@"LeafletPageContentViewController" bundle:nil];
dataViewController.dataObject = [self.modelArray objectAtIndex:index];
return dataViewController;
}
Ответ 4
Ответ на @milijan отлично работал. Вот быстрая версия его ответа:
func jumptoPage(index : Int) {
let vc = viewControllerAtIndex(index)
let direction : UIPageViewControllerNavigationDirection!
if currentIndex < index {
direction = UIPageViewControllerNavigationDirection.Forward
}
else {
direction = UIPageViewControllerNavigationDirection.Reverse
}
if (currentIndex < index) {
for var i = 0; i <= index; i++ {
if (i == index) {
self.newPageViewController.setViewControllers([vc], direction: direction, animated: true, completion: nil)
}
else {
self.newPageViewController.setViewControllers([viewControllerAtIndex(i)], direction: direction, animated: false, completion: nil)
}
}
}
else {
for var i = currentIndex; i >= index; i = i - 1 {
if i == index {
self.newPageViewController.setViewControllers([vc], direction: direction, animated: true, completion: nil)
}
else {
self.newPageViewController.setViewControllers([viewControllerAtIndex(i)], direction: direction, animated: false, completion: nil)
}
}
}
currentIndex = index
}
Ответ 5
Swift Code:
func goToPage(index: Int) {
if index < viewControllers.count {
pageVC!.setViewControllers([viewControllers[index]], direction: .Forward, animated: true, completion: nil)
}
}