2016-08-15 2 views
0

UIPageViewController을 처음 사용하여 Objective-C에 대한 설명을 찾을 수 없습니다. 나는 독자적으로 하나를 구현하려했지만, 실행시 검은 색 화면 만 보여줍니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?UIPageView 구현 검은 색 화면

저는 초기 ViewController 인 IB에서 설정된 ViewController와 아래에있는 두 개의 스토리 보드 ID가 있고 다른 곳에 연결되지 않고 그냥 앉아있는 두 개의 다른 것들이 있습니다. 여기

내 최초의 ViewController에 대한 코드입니다 : 당신이 누락

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil]; 
    [self addChildViewController:self.pageViewController]; 
    self.pageViewController.delegate = self; 
    self.pageViewController.dataSource = self; 

    UIViewController *vc1 = [self.storyboard instantiateViewControllerWithIdentifier:@"vc1"]; 
    UIViewController *vc2 = [self.storyboard instantiateViewControllerWithIdentifier:@"vc2"]; 

    viewControllers = [[NSArray alloc] initWithObjects:vc1,vc2, nil]; 

    [self.pageViewController setViewControllers:@[vc1] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil]; 

} 

-(UIViewController *)viewControllerAtIndex:(NSUInteger)index 
{ 
    return viewControllers[index]; 
} 

-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController 
    viewControllerBeforeViewController:(UIViewController *)viewController 
{ 
    NSUInteger currentIndex = [viewControllers indexOfObject:viewController]; 

    --currentIndex; 
    currentIndex = currentIndex % (viewControllers.count); 
    return [viewControllers objectAtIndex:currentIndex]; 
} 

-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController 
     viewControllerAfterViewController:(UIViewController *)viewController 
{ 
    NSUInteger currentIndex = [viewControllers indexOfObject:viewController]; 

    ++currentIndex; 
    currentIndex = currentIndex % (viewControllers.count); 
    return [viewControllers objectAtIndex:currentIndex]; 
} 

-(NSInteger)presentationCountForPageViewController: 
(UIPageViewController *)pageViewController 
{ 
    return viewControllers.count; 
} 

-(NSInteger)presentationIndexForPageViewController: 
(UIPageViewController *)pageViewController 
{ 
    return 0; 
} 
+0

이를 확인하실 수 있습니다 - https://github.com/mindsnacks/MSPageViewController 어쩌면 도움이됩니다. – Ro4ch

+1

pageViewController를 화면에 추가 한 지점을 확인할 수 없습니다. –

+0

여기 있습니다 : [self addChildViewController : self.pageViewController]; –

답변

0

addSubview

그리고 그 후, 당신은

didMoveToParentViewCon를 호출해야합니다

컨트롤러

자식보기 컨트롤러에서 수명주기 이벤트를 가져 오는 데 사용됩니다.

@interface ViewController()<UIPageViewControllerDelegate, UIPageViewControllerDataSource> 
@property (nonatomic, strong) UIPageViewController* pageViewController; 
@property (nonatomic,strong) NSArray *viewControllers; 
@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil]; 
    [self addChildViewController:self.pageViewController]; 
    [self.view addSubview:self.pageViewController.view]; 
    [self.pageViewController didMoveToParentViewController:self]; 
    self.pageViewController.delegate = self; 
    self.pageViewController.dataSource = self; 

    UIViewController *vc1 = [[UIViewController alloc] init]; 
    vc1.view.backgroundColor = [UIColor blueColor]; 
    UIViewController *vc2 = [[UIViewController alloc] init]; 
    vc2.view.backgroundColor = [UIColor redColor]; 

    self.viewControllers = [[NSArray alloc] initWithObjects:vc1,vc2, nil]; 

    [self.pageViewController setViewControllers:@[vc1] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil]; 

} 

-(UIViewController *)viewControllerAtIndex:(NSUInteger)index 
{ 
    return self.viewControllers[index]; 
} 

-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController 
    viewControllerBeforeViewController:(UIViewController *)viewController 
{ 
    NSUInteger currentIndex = [self.viewControllers indexOfObject:viewController]; 

    --currentIndex; 
    currentIndex = currentIndex % (self.viewControllers.count); 
    return [self.viewControllers objectAtIndex:currentIndex]; 
} 

-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController 
     viewControllerAfterViewController:(UIViewController *)viewController 
{ 
    NSUInteger currentIndex = [self.viewControllers indexOfObject:viewController]; 

    ++currentIndex; 
    currentIndex = currentIndex % (self.viewControllers.count); 
    return [self.viewControllers objectAtIndex:currentIndex]; 
} 

-(NSInteger)presentationCountForPageViewController: 
(UIPageViewController *)pageViewController 
{ 
    return self.viewControllers.count; 
} 

-(NSInteger)presentationIndexForPageViewController: 
(UIPageViewController *)pageViewController 
{ 
    return 0; 
} 
@end 
+0

@ Balázs Vincze 내 대답을 시도 했습니까? – MCMatan

0
- (void)viewDidLoad { 
    [super viewDidLoad]; 


    self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil]; 

    self.pageController.dataSource = self; 
    self.pageController.delegate = self; 

    [NSTimer scheduledTimerWithTimeInterval:5.0 
            target:self 
            selector:@selector(loadNextController) 
            userInfo:nil 
            repeats:YES]; 


    [[self.pageController view] setFrame:[[self sliderView] bounds]]; 

    self.pageControl.numberOfPages = 5; 
    self.pageControl.currentPage = 0; 

    PageChildVC *initialViewController = [self viewControllerAtIndex:0]; 

    NSArray *viewControllers = [NSArray arrayWithObject:initialViewController]; 

    [self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil]; 

    [self addChildViewController:self.pageController]; 
    [[self sliderView] addSubview:[self.pageController view]]; 
    [self.pageController didMoveToParentViewController:self]; 
} 


- (PageChildVC *)viewControllerAtIndex:(NSUInteger)index { 
    if ((5 == 0) || (index >= 5)) { 
     return nil; 
    } 
    PageChildVC *childViewController = [[PageChildVC alloc] initWithNibName:@"PageChildVC" bundle:nil]; 
    childViewController.index = index; 
    return childViewController; 

} 
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController { 
    NSUInteger index = ((PageChildVC*) viewController).index; 
    if ((index == 0) || (index == NSNotFound)) { 
     index = 5; 
    } 
    index--; 
    return [self viewControllerAtIndex:index]; 
    } 
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController { 
    NSUInteger index = ((PageChildVC*) viewController).index; 
    if (index == NSNotFound) { 
     return nil; 
    } 
    index++; 
    if (index == 5) { 
     index = 0; 
    } 
    return [self viewControllerAtIndex:index]; 
    } 
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController { 
    return 5; 
} 
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController { 
    return [(PageChildVC *)[self.pageController.viewControllers firstObject] index]; 
} 
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed 
{ 
    PageChildVC *currentViewController = pageViewController.viewControllers[0]; 
    currentIndex = (int)currentViewController.index; 
    [self.pageControl setCurrentPage:currentIndex]; 
} 
- (void)loadNextController { 
    currentIndex++; 
    PageChildVC *nextViewController = [self viewControllerAtIndex:currentIndex]; 
    if (nextViewController == nil) { 
     currentIndex = 0; 
     nextViewController = [self viewControllerAtIndex:currentIndex]; 
    } 
    [self.pageController setViewControllers:@[nextViewController] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil]; 
    [self.pageControl setCurrentPage:currentIndex]; 
}