2011-12-07 36 views
4

我刚刚“尝试”去了PageControl的苹果教程。现在我应该指出,我没有完全理解这一点,它似乎很复杂,所以我很抱歉,如果这个问题很明显。UIPageControl加载新的视图或不同的控制器

我注意到苹果从.plist中加载了它的内容。现在,如果你只有一个UILabel和一个UIImageView,那么所有这些都很好,很容易,但是如果我做了一些更复杂的事情呢?有如果我想每个“页”是什么样的14级不同的变量,但这别的东西取决于你是哪一页每“页”上的按钮...

所以我的问题是这样的(也许这不会首先要做的就是聪明的): 有没有办法编写它,所以当用户切换页面时,它会加载一个不同的控制器,它恰好拥有自己的.Xib文件和已经在界面构建器中创建的视图?

谢谢

回答

0

是的。您将使用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 

这就是它!

相关问题