2013-03-07 4 views
0

RootViewController, DataViewController 및 ModelController를 만드는 xcode의 페이지 기반 응용 프로그램 템플릿을 사용하고 있습니다.DataViewController에서 UIPageViewController 페이지로 이동 하시겠습니까?

내가 RootViewController에서 필요한 페이지로 이동하도록 만들었지 만 특정 페이지의 이벤트에 대해 트리거 된 DataViewController에서도 필요합니다. 그렇게 잘 작동하지 않습니다. DataViewController에서 작동하도록 코드를 어떻게 변경해야합니까?

내 코드는 다음과 같습니다 -from 루트 (작업) :

//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]; 
} 

-from 페이지, dataviewcontroller (작동하지 않는) :

DataViewController *targetPageViewController = [((UIPageViewController*)self.parentViewController) viewControllerAtIndex:(pageToGoTo - 1) storyboard:self.storyboard]; //The problem is here, but I do not know how to solve it. 


//put it(or them if in landscape view) in an array 
NSArray *theViewControllers = nil; 
theViewControllers = [NSArray arrayWithObjects:targetPageViewController, nil]; 


[((UIPageViewController*)self.parentViewController) setViewControllers: theViewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil]; 

답변

1

내가 여기 https://stackoverflow.com/a/16435166/1463518이 질문에 대답

내가 한 일은 알림을 사용하는 것입니다.

01 23,516,

는 내가있는 viewDidLoad에 RootViewController DataViewController

[[NSNotificationCenter defaultCenter]postNotificationName:@"H2RAutoPage" object:nil]; 

과의 버튼 클릭에 알림을 추가

내 자동 페이지 루틴이 나는 또한 한이

- (void)autoPage: (NSNotification *)notification 
{ 
//get current index of current page 
    NSUInteger currentPage = [self.modelController indexOfViewController:[self.pageViewController.viewControllers objectAtIndex:0]]; 
// this is the next page nil mean we have reach the end 
    H2RDataViewController *targetPageViewController = [self.modelController viewControllerAtIndex:(currentPage + 1) storyboard:self.storyboard]; 
    if (targetPageViewController) { 
     //put it(or them if in landscape view) in an array 
     NSArray *viewControllers = [NSArray arrayWithObjects:targetPageViewController, nil]; 
     //add page view 
     [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL]; 
    } 

} 

과 같은

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(autoPage:) name:@"H2RAutoPage" object:nil]; 

델리게이트를 사용해보십시오. 그러나 NSnotification을 훨씬 더 깨끗하게 발견 한 대표를 잃어 버렸기 때문에 코드가 지저분 해졌습니다.

+0

감사합니다. 대리인을 사용했습니다. 그것이 그 당시에 무엇인지 알지 못했습니다. 나는이 해결책이 또한 작동 할 수 있었다는 것을 짐작한다. –

관련 문제