2014-12-03 79 views

回答

1

在頁面內容視圖控制器中,有一個名爲pageIndex(NSUInteger)的屬性。

頁面瀏覽控制器在製作新內容頁面時設置此屬性。

假設你有兩個內容頁面,那麼第一個頁面的pageIndex屬性爲0,下一個將爲1. 所有你需要做的就是檢查pageIndex是否等於0(表示它是第一個內容頁面)並實現該頁面的按鈕。

假設你知道如何使一個按鈕,頁面內容視圖控制器把這個在你目前的viewDidLoad代替:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    if(self.indexNumber == 0) { 
     // code to make your button. 
    } 
} 
+0

我如何做到這一點的財產? – 2014-12-03 11:59:04

+0

爲你編輯答案,這是否足夠清楚? – 2014-12-03 12:08:29

+0

與示例相關的任何示例代碼? – 2014-12-03 12:11:51

1

在MainViewController.m文件

- (void)viewDidLoad 
{ 

//PageView Controller Code 
    UIPageViewController *pageVcObject = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil]; 

    pageVcObject.dataSource = self; 
    pageVcObject.delegate = self; 

    CGRect frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height-100); 
// [pageVcObject.view setFrame:self.view.bounds]; 
    [pageVcObject.view setBackgroundColor:[UIColor clearColor]]; 
    [pageVcObject.view setFrame:frame]; 
    ChildVcForPageViewController *initialViewController = [self viewControllerAtIndex:0]; 

    NSArray *viewControllers = [NSArray arrayWithObject:initialViewController]; 
    [pageVcObject setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil]; 

    [self addChildViewController:pageVcObject 
    ]; 
    [[self view] addSubview:[pageVcObject view]]; 
    [pageVcObject didMoveToParentViewController:self]; 
} 

的#pragma標記 - UIPageViewControllerDataSource方法

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(ChildVcForPageViewController *)viewController 

{ 


    NSInteger index = [viewController index]; 
    pageControl.currentPage = index; 
    index++; 



    if (index == imagesArray.count) 
     return nil; 
    else 
    { 

     return [self viewControllerAtIndex:index]; 
    } 



} 

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(ChildVcForPageViewController *)viewController 

{ 

    NSInteger index = [viewController index]; 
    pageControl.currentPage = index; 

    if (index == 0) return nil; 

    index--; 


    return [self viewControllerAtIndex:index]; 
} 

- (ChildVcForPageViewController *)viewControllerAtIndex:(NSUInteger)indexValue 
     { 
      ChildVcForPageViewController *childViewController = [[ChildVcForPageViewController alloc] init]; 

      childViewController.index = indexValue; 

      return childViewController; 
     } 

在ChildVcForPageViewController.h聲明類似下面

@property (assign, nonatomic) NSInteger index; 

然後添加在ChildVcForPageViewController.m代碼文件

- (void)viewDidLoad 

{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

    if(self.index == 0) { 
     // code to make your button. 

UIButton *buttonz = [UIButton buttonWithType:UIButtonTypeCustom]; 
     buttonz.frame = CGRectMake(160, 0, 160, 30); 
     [buttonz setTitle:@"Charge" forState:UIControlStateNormal]; 
     [buttonz addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventTouchUpInside]; 


     //add the button to the view 
     [backImageView addSubview:buttonz]; 
    } 
}