UIPageViewController - 如何跳转到特定的页码?

时间:2012-07-04 20:12:10

标签: objective-c ios cocoa-touch uipageviewcontroller

我花了将近8个小时来了解如何跳转到UIPageViewController中的特定页码...下面是我的项目的样子

我想创建一个看起来像Ibooks的应用程序---

我已经从这里提供的代码中获得了帮助 - http://www.ioslearner.com/tag/uipageviewcontroller-sample-code/

我制作了一个plist和UITableView,我从TableView中选择了值,并在UIPAgeiewController上的webView上显示相同的内容,但问题是只有Web视图中的页面发生了变化,而不是UIPageViewController的实际页面编号。 ..

如果我缩小我的问题,它看起来像---是否有办法在UIPageViewController中切换页码.... ???

5 个答案:

答案 0 :(得分:5)

您必须实例化管理要跳转到的页面的视图控制器,然后调用页面视图控制器的setViewControllers:direction:animated:completion:方法,并传递新的视图控制器。

答案 1 :(得分:3)

这是另一个如何使用已解析的索引跳转到页面的示例:

-(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;
}

答案 2 :(得分:3)

Swift Code:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) Data.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

//begin eww gross
        Vehicle temp;
        try {
            temp = Data.getVehicle(values.get(position).getId());
        } catch (ObjectNotFoundException e) {
            Log.e(TAG, e.getMessage() + "-- unable to find vehicle while getting list item vehicle for vehicles tab fragment");
            temp = values.get(position);
        }
        final Vehicle vehicle = temp;
//end disappointing, ugly workaround

        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.vehicle_item_with_img, null);
            holder = new ViewHolder();
            holder.vehicleLabel = (TextView) convertView.findViewById(R.id.vehicle_label);
            holder.statusImage = (ImageView) convertView.findViewById(R.id.lmImageView);
            holder.sImage = (ImageView) convertView.findViewById(R.id.sImageView);
            holder.vehicleTag = vehicle.getId();
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        if (Data.isFollowingVehicle(vehicle)) {
            holder.sImage.setVisibility(View.VISIBLE);
        }
        else {
            holder.sImage.setVisibility(View.INVISIBLE);
        }

        holder.statusImage.setImageDrawable(vehicle.getListIcon());
        holder.vehicleLabel.setText(vehicle.getFormattedLabel());

        final ViewHolder finalHolder = holder;
        convertView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (!vehicle.isFollowed()) {
                    finalHolder.sImage.setVisibility(View.VISIBLE);
                    mapInterface.follow(vehicle);
                } else {
                    finalHolder.sImage.setVisibility(View.GONE);
                    mapInterface.unfollow(vehicle);
                }
                return false;
            }
        });

        convertView.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                mapInterface.handleVehicleClickFromList(vehicle);
            }
        });

        return convertView;
    }

答案 3 :(得分:2)

我使用此功能(我总是处于横向,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 :(得分:2)

@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
}