3

独自のView ControllerをUIPageViewControllerメソッドに手動で追加するために何が欠けているか知っている人はいますか?

私は現在これを持っており、続行する方法がわかりません:

NSDictionary *pageViewOptions = [NSDictionary dictionaryWithObjectsAndKeys:UIPageViewControllerOptionSpineLocationKey, [NSNumber numberWithInteger:UIPageViewControllerSpineLocationMin], nil];

self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:pageViewOptions];


BookViewController *pageOne = [self.storyboard instantiateViewControllerWithIdentifier:@"BookViewController"];

AnotherPage *pageTwo = [self.storyboard instantiateViewControllerWithIdentifier:@"AnotherPage"];

PageThree *pageThree = [self.storyboard instantiateViewControllerWithIdentifier:@"PageThree"];


self.pageViewController.delegate = self;
self.pageViewController.dataSource = self;

[self.pageViewController setViewControllers:[NSArray arrayWithObjects:pageOne, pageTwo, pageThree, nil] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

[self addChildViewController:self.pageViewController];

[self.view addSubview:self.pageViewController.view];

self.view.gestureRecognizers = self.pageViewController.gestureRecognizers;

[self.pageViewController didMoveToParentViewController:self];

しかし、うまくいかないようです。RootViewController のみが表示されます。

私のような熱心な初心者を前もって助けてくれる皆さんに感謝します.... :)

4

3 に答える 3

7

これを設定した方法は、まったく機能しませんUIPageViewController

最初に理解しておくべきことは、ユーザーに表示するすべてのビュー コントローラーではなく、表示可能setViewControllers:direction:animated:completionなビュー コントローラーのみを設定することです。(つまり、コードのように 3 つのコントローラーの配列を設定しようとすると、例外が発生します。)

次に、表示可能なページが複数ある場合 (スパインの位置によっては 2 つある場合もあります)、ページ ビュー コントローラーが表示するビュー コントローラーを提供するプロトコルを実装する必要があります。UIPageViewControllerDataSource

短い例を次に示します ( のサブクラスとして実装されていますが、子ページ ビュー コントローラーでも動作しUIPageViewControllerます... に置き換えるだけです) 。self.dataSource = ...myPageViewController.dataSource = ...

@implementation MyPageViewController {
    // we keep our page view controllers in an array, but we could just as easily
    // instantiate them on the fly in the data source methods
    NSArray *_pages;
}

- (void)setupPages {
    /*
     * set up three pages, each with a different background color
     */
    UIViewController *a = [[UIViewController alloc] initWithNibName:nil bundle:nil];
    a.view.backgroundColor = [UIColor redColor];
    UIViewController *b = [[UIViewController alloc] initWithNibName:nil bundle:nil];
    b.view.backgroundColor = [UIColor greenColor];
    UIViewController *c = [[UIViewController alloc] initWithNibName:nil bundle:nil];
    c.view.backgroundColor = [UIColor blueColor];
    _pages = @[a, b, c];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupPages];
    self.dataSource = self;
    // set the initially visible page's view controller... if you don't do this
    // you won't see anything.
    [self setViewControllers:@[_pages[0]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:^(BOOL finished) {
    }];
}

#pragma mark - UIPageViewControllerDataSource

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    if (nil == viewController) {
        return _pages[0];
    }
    NSInteger idx = [_pages indexOfObject:viewController];
    NSParameterAssert(idx != NSNotFound);
    if (idx >= [_pages count] - 1) {
        // we're at the end of the _pages array
        return nil;
    }
    // return the next page's view controller
    return _pages[idx + 1];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    if (nil == viewController) {
        return _pages[0];
    }
    NSInteger idx = [_pages indexOfObject:viewController];
    NSParameterAssert(idx != NSNotFound);
    if (idx <= 0) {
        // we're at the end of the _pages array
        return nil;
    }
    // return the previous page's view controller
    return _pages[idx - 1];
}
于 2013-07-09T22:42:08.470 に答える
0

この機能を試してください:

setViewControllers:direction:animated:completion:

表示するView Controllerを設定します。

- (void)setViewControllers:(NSArray *)viewControllers direction:(UIPageViewControllerNavigationDirection)direction animated:(BOOL)animated completion:(void (^)(BOOL finished))completion

パラメーター

viewControllers: 表示されるビュー コントローラーまたはビュー コントローラー。

direction: ナビゲーションの方向。

animation: トランジションをアニメーション化するかどうかを示すブール値。

完了: ページめくりのアニメーションが完了したときに呼び出されるブロック。

このブロックは次のパラメーターを取ります。

アニメーションが終了した場合は YES。スキップされた場合は NO。

于 2012-06-29T07:30:13.497 に答える
0

ここにはいくつかの問題があります。私は少し初心者なので、これでコードのすべてが修正されるかどうかはわかりません。

  1. UIPageViewController が必要であり、必要に応じてインスタンス化される個々のページのビューが必要です。ビューの配列を渡すのは効率的ではありません (メモリを使いすぎます)。

  2. UIPageViewController のサブクラスであるクラスに UIPageViewControllerDataSource プロトコルを実装する必要があります。スニペットのコードのほとんど (すべてではない) がここに配置されます。説明については、クラス リファレンスまたはこのチュートリアルを参照してください。

  3. おそらく、ジェスチャ レコグナイザーで必要以上の作業を行っていることでしょう。IBOutlet を使用する場合は、インターフェイス ビルダーを介して配管を行うことができます。

お役に立てれば。

于 2011-12-06T03:22:10.900 に答える