2011-12-07 33 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 

這就是它!

相關問題