UIPageControl加载新视图或不同的控制器

时间:2011-12-07 02:11:28

标签: objective-c ios xcode uipagecontrol

我刚刚“尝试”浏览PageControl的苹果教程。现在我应该指出,我并没有完全理解这一点,它似乎很复杂,所以如果这个问题非常明显,我道歉。

我注意到苹果从.plist加载了它的内容。如果你拥有的只有一个UILabel和一个UIImageView,那就好了又简单但是如果我做了更复杂的事情呢?如果我希望每个“页面”都有14个不同的变量,每个“页面”上的一个按钮,根据您所在的页面执行其他操作,该怎么办...

所以我的问题是这个(也许这首先做起来并不聪明): 有没有什么可以对它进行编码,所以当用户切换页面时,它会加载一个不同的控制器,该控制器恰好有自己的.Xib文件和视图已经在界面构建器中创建了?

谢谢

1 个答案:

答案 0 :(得分:1)

是的。您将使用UIPageViewControllerUIPageViewController具有根据用户是向左还是向右滑动而调用的数据源和委托方法。它基本上说“嘿,给我UIViewController我应该在这个UIViewController之前或之后显示。”

以下是一个示例:

<强> MyPageViewController.h

@interface MyPageViewController : UIPageViewController <UIPageViewControllerDataSource, UIPageViewControllerDelegate>

@end

<强> MyPageViewController.m

#import "MyPageViewController.h"

@implementation MyPageViewController 

- (id)init
{
    self = [self initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll
                   navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
                                 options:nil];

    if (self) {
        self.dataSource = self;
        self.delegate = self;
        self.title = @"Some title";

        // set the initial view controller
        [self setViewControllers:@[[[SomeViewController alloc] init]]
                       direction:UIPageViewControllerNavigationDirectionForward
                        animated:NO
                      completion:NULL];
    }

    return self;
}

#pragma mark - UIPageViewController DataSource methods
- (UIViewController *)pageViewController:(UIPageViewController *)pvc
      viewControllerBeforeViewController:(UIViewController *)vc
{
    // here you put some logic to determine which view controller to return.
    // You either init the view controller here or return one that you are holding on to
    // in a variable or array or something.
    // When you are "at the end", return nil

    return nil;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pvc
       viewControllerAfterViewController:(UIViewController *)vc
{
    // here you put some logic to determine which view controller to return.
    // You either init the view controller here or return one that you are holding on to
    // in a variable or array or something.
    // When you are "at the end", return nil

    return nil;
}

@end

就是这样!