In this tutorial,是否可以只在第一个视图中添加一个按钮,即index == 0? 如果是,怎么样?
答案 0 :(得分:1)
在页面内容视图控制器中,有一个名为pageIndex(NSUInteger)的属性。
在制作新内容页面时,网页浏览控制器会设置此属性。
所以,让我们说你有2个内容页面,然后第一个页面的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.
}
}
答案 1 :(得分: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 mark - 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];
}
}