Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
138 views
in Technique[技术] by (71.8m points)

ios - UIPageViewController setViewController causing app to crash

I have a - (void)setViewControllers:(NSArray *)viewControllers direction:(UIPageViewControllerNavigationDirection)direction animated:(BOOL)animated completion:(void (^)(BOOL finished))completion set up in a method. Whenever the method is called it doesn't go to the next page. Instead, it goes to the UIPageViewController storyboard and then crashes. I'm not sure what I'm doing wrong. I am using MSPageViewController for the pageviewcontroller, could that be it?

Heres my code:

UIViewController *viewcont = [[UIViewController alloc]init];

NSArray *viewControllers = [NSArray arrayWithObject:viewcont];

[self setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];

Thanks.

There are 3 storyboards all conforming to MSPageViewControllerChild with the pageIndex property synthesized. IntroPageViewController is the first storyboard (p1).

PagingViewController.h:

    //
//  PagingViewController.m
//  MordechaiLevi
//
//  Created by Mordechai Levi on 4/10/14.
//  Copyright (c) 2014 Mordechai Levi. All rights reserved.
//

#import "PagingViewController.h"
#import "IntroPageViewController.h"
#import "MSPageViewController.h"

@interface PagingViewController ()

@end

@implementation PagingViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.device = [UIDevice currentDevice];

    self.device.proximityMonitoringEnabled = YES;

    if (self.device.proximityMonitoringEnabled == YES) {

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

    }else{

        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Uh Oh!" message:@"To use this app you need a device with a proximity sensor." delegate:self cancelButtonTitle:@"Got it" otherButtonTitles:nil, nil];

        [alert show];
    }

    self.view.backgroundColor = [UIColor colorWithRed:0.2 green:0.859 blue:0.643 alpha:1.0];

}


- (UIStatusBarStyle)preferredStatusBarStyle {

    return UIStatusBarStyleLightContent;
}

- (void)sensorCovered {

    if (self.device.proximityState == YES) {

        IntroPageViewController *viewcont = [[IntroPageViewController alloc]init];

        NSArray *viewControllers = [NSArray arrayWithObject:viewcont];

        [self setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];


        NSLog(@"sensor covered");

    }else{

        NSLog(@"sensor not covered");
    }
}




- (NSArray *)pageIdentifiers {

    return @[@"p1", @"p2", @"p3"];
}


@end
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Looks like you're using MSPageViewController, with a controller that doesn't conform to MSPageViewControllerChild.

From the documentation:

Each of them [controllers] must be a class that conforms to MSPageViewControllerChild (if you don't need to add any extra functionality to it you can use MSPageViewControllerPage).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...