2014-12-03 82 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]; 
    } 
}