我有基于页面的应用程序,带有工具栏按钮。当按下该按钮时,它有一个包含10个页面列表的tableview弹出框,当选择其中一个单元格时,我希望应用程序转到所选编号的页面。
这是我的PopOverViewController视图中获取所选单元格的部分:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger selectedRow;
selectedRow = indexPath.row;
RootViewController *rootViewController = [[RootViewController alloc]init];
[rootViewController turnToPage:selectedRow];
}
这是它调用的方法(在RootViewController中):
- (void)turnToPage:(NSInteger)sender
{
NSUInteger pageToGoTo = sender;
//get current index of current page
DataViewController *theCurrentViewController = [self.pageViewController.viewControllers objectAtIndex:0];
NSUInteger retreivedIndex = [self.modelController indexOfViewController:theCurrentViewController];
//get the page(s) to go to
DataViewController *targetPageViewController = [self.modelController viewControllerAtIndex:(pageToGoTo - 1) storyboard:self.storyboard];
//put it(or them if in landscape view) in an array
NSArray *theViewControllers = nil;
theViewControllers = [NSArray arrayWithObjects:targetPageViewController, nil];
//check which direction to animate page turn then turn page accordingly
if (retreivedIndex < (pageToGoTo - 1) && retreivedIndex != (pageToGoTo - 1)){
[self.pageViewController setViewControllers:theViewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL];
}
if (retreivedIndex > (pageToGoTo - 1) && retreivedIndex != (pageToGoTo - 1)){
[self.pageViewController setViewControllers:theViewControllers direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:NULL];
}
}
方法代码基于我在这里找到的解决方案: Is it possible to Turn page programmatically in UIPageViewController?
然而,即使代码成功运行,它也不会更改页面。我见过类似的问题,但找不到有效的解决方案。需要注意的一点是,我的pageviewcontroller处于横向模式,一次显示一个页面。
任何人都可以光明吗? 谢谢!