2013-03-13 2 views
0

UIPageViewController을 구현하려고합니다. 나는 4 개의 분리 된 viewController를 생성하여 스토리 보드에 추가했다. 이제 UIPageViewController으로 스크롤하고 싶습니다. 이것이 내가 코드에서하는 일입니다. PageviewController는 항상 2147483648과 같은 색인을 반환합니다.

나는 그것을에서 modelArray에게 넣어 모든 VC를 만든 다음 UIPageViewController

self.modelArray = [[NSMutableArray alloc] init]; 
    NSMutableArray *pageData = [[NSMutableArray alloc]init]; 
    ViewOneViewController *viewOne = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewOne"]; 
    ViewTwoViewController *viewTwo = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewTwo"]; 
    ViewThreeViewController *viewThree = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewThree"]; 
    ViewFourViewController *viewFour = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewFour"]; 

    [pageData addObject:viewOne]; 
    [pageData addObject:viewTwo]; 
    [pageData addObject:viewThree]; 
    [pageData addObject:viewFour]; 

    self.modelArray = pageData; 

다음 설치 기본 및 초기 VC 설정 :이 내 delegate 방법 (있는 마지막

ViewOneViewController *cVC = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewOne"]; 
    NSArray *viewControllers = [NSArray arrayWithObject:cVC]; 
    [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil]; 

    // Add the pageViewController as the childViewController 
    [self addChildViewController:self.pageViewController]; 

    // Add the view of pageViewController to the currentView 
    [self.view addSubview:self.pageViewController.view]; 

    // Call didMoveToParentViewController: of the childViewController, the UIPageViewController instance in our case. 
    [self.pageViewController didMoveToParentViewController:self]; 

    // Assign the gestureRecognizers property of the pageViewController to the view's gestureRecognizers property 
    self.view.gestureRecognizers = self.pageViewController.gestureRecognizers; 

을 예를 들면, 다음 VC를위한 방법)

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController 
     viewControllerAfterViewController:(UIViewController *)viewController 
{ 
    NSUInteger currentIndex = [self.modelArray indexOfObject:viewController]; 
    if(currentIndex == self.modelArray.count - 1) 
     return nil; 

    UIViewController *cVC = [self.modelArray objectAtIndex:currentIndex + 1]; 
    return cVC; 
} 

Bu 내가 빌드하고 실행할 때 다음 오류가 발생합니다.

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2147483648 beyond bounds [0 .. 3]' 

아무도 도움이 될 수 있습니까?

종류에 관해서 .. 편집 여기

thread #1: tid = 0x2403, 0x37776350 libsystem_kernel.dylib`__pthread_kill + 8, stop reason = signal SIGABRT 
    frame #0: 0x37776350 libsystem_kernel.dylib`__pthread_kill + 8 
    frame #1: 0x33115122 libsystem_c.dylib`pthread_kill + 58 
    frame #2: 0x33151972 libsystem_c.dylib`abort + 94 
    frame #3: 0x36f2fd4e libc++abi.dylib`abort_message + 74 
    frame #4: 0x36f2cff8 libc++abi.dylib`default_terminate() + 24 
    frame #5: 0x3165ba76 libobjc.A.dylib`_objc_terminate() + 146 
    frame #6: 0x36f2d07a libc++abi.dylib`safe_handler_caller(void (*)()) + 78 
    frame #7: 0x36f2d114 libc++abi.dylib`std::terminate() + 20 
    frame #8: 0x36f2e598 libc++abi.dylib`__cxa_rethrow + 88 
    frame #9: 0x3165b9d0 libobjc.A.dylib`objc_exception_rethrow + 12 
    frame #10: 0x38c82f20 CoreFoundation`CFRunLoopRunSpecific + 456 
    frame #11: 0x38c82d48 CoreFoundation`CFRunLoopRunInMode + 104 
    frame #12: 0x34bbc2ea GraphicsServices`GSEventRunModal + 74 
    frame #13: 0x35c14300 UIKit`UIApplicationMain + 1120 
    frame #14: 0x0005050c Franke2`main(argc=1, argv=0x2fdb1d20) + 116 at main.m:16 
    frame #15: 0x340ccb20 libdyld.dylib`start + 4 
+0

스택 추적을 제공하십시오. – trojanfoe

+0

@trojanfoe stackTrace의 의미는 무엇입니까? 오류를 추가했습니다. – Steaphann

+0

크래시가 발생하고 앱이 Xcode에서 멈춘 경우 디버거 콘솔에 'bt'를 입력하고 생성 된 내용을 [질문과 함께 게시]합니다. – trojanfoe

답변

0
ViewOneViewController *cVC = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewOne"]; 
NSArray *viewControllers = [NSArray arrayWithObject:cVC]; 
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil]; 

당신은 ViewOneViewController의 새로운 객체를 생성하고 pageViewController의 viewControllers 배열에 추가하고. 이 cVC 객체는 이전에 modelArray에 추가 한 객체와 아무 관련이 없습니다. 이것들은 모두 동일한 클래스의 다른 객체, 즉 ViewOneViewController입니다.

은 따라서 당신은 당신이 instantiateViewControllerWithIdentifier:

This method creates a new instance of the specified view controller each time you call it.

의 논의에서

NSUInteger currentIndex = [self.modelArray indexOfObject:viewController]; //will return garbage value in your case 

을 수행 할 때 쓰레기 값을받지 않습니다 있도록

+0

그래서 NSArray * viewControllers = [[NSArray alloc] initwithArray : modelArray];라고 말해야합니다. ? – Steaphann

+0

어떨까요 [self.pageViewController setViewController : self.modelArray direction : UIPageViewControllerNavigationDirectionForward animated : 아니요 완료 : 없음]; –

+0

그런 다음이 오류가 발생합니다. '제공된보기 컨트롤러 수 (4)가 요청 된 전환에 필요한 수 (1)와 일치하지 않습니다.' – Steaphann

2

2147483648가 NSNotFound입니다 pageViewController의 viewControllers 배열에 modelArray에서 같은 오브젝트를 통과해야 객체가 없으면 -[NSArray indexOfObject:]이 반환합니다. 당신의 라인

NSUInteger currentIndex = [self.modelArray indexOfObject:viewController]; 

viewController에 따라서

modelArray에 있지 않습니다. 이는 매번 View Controller의 새 인스턴스를 작성하기 때문입니다.

보조 노트에서 self.modelArray = [[NSMutableArray alloc] init];을 수행하고 그 배열로 아무 작업도 수행하지 않고 self.modelArray = pageData;을 던지면서 던져 버립니다. 방금 하나를 사용할 때 두 개의 빈 배열을 만들 필요가 없습니다.

0

indexOfObject: 메서드를 호출 한 후에 반환 된 인덱스가 NSNotFound이 아닌지 확인해야합니다. 마녀는 배열에 개체가없는 반환 값입니다.

코드가 더 비슷해야합니다.

NSUInteger currentIndex = [self.modelArray indexOfObject:viewController]; 
if(currentIndex == NSNotFound || 
    currentIndex == self.modelArray.count - 1) 
    return nil; 

앞으로 당신은 Xcode exception breakpoint를 사용하여이 버그 자신을 발견 할 수 있어야한다.

+0

이 코드는 컴파일되지 않습니다. 닫는 괄호와 중괄호 쌍이 없습니다. –

관련 문제