【问题标题】:UIPageControl loading new view or different controllerUIPageControl 加载新视图或不同的控制器
【发布时间】:2011-12-07 02:11:27
【问题描述】:
我刚刚“尝试”浏览了 PageControl 的苹果教程。现在我应该指出,我没有完全理解这一点,它看起来很复杂,所以如果这个问题很明显,我很抱歉。
我注意到苹果从 .plist 加载了它的内容。现在,如果您只有一个 UILabel 和一个 UIImageView,那就太好了,但是如果我想做一些更复杂的事情怎么办?如果我希望每个“页面”有 14 个不同的变量,每个“页面”上的一个按钮会根据您所在的页面执行其他操作...
所以我的问题是这样的(也许一开始这样做并不明智):
有没有办法对其进行编码,以便当用户切换页面时,它会加载一个不同的控制器,该控制器恰好有自己的 .Xib 文件和已在界面构建器中创建的视图?
谢谢
【问题讨论】:
标签:
objective-c
ios
xcode
uipagecontrol
【解决方案1】:
是的,有。您将使用UIPageViewController。 UIPageViewController 具有数据源和委托方法,根据用户是向左还是向右滑动来调用它们。它基本上说“嘿,给我一个 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
就是这样!