我想知道如何在UIPageViewController
不同 UIWebView 的 URL 的每个页面上显示,假设第一个 pdf 是 one.pdf,第二个是 two.pdf 等等...
我在 Xcode 4.2 中使用 UIPageViewController
我想知道如何在UIPageViewController
不同 UIWebView 的 URL 的每个页面上显示,假设第一个 pdf 是 one.pdf,第二个是 two.pdf 等等...
我在 Xcode 4.2 中使用 UIPageViewController
最好的方法是创建一个自定义 viewController 子类。
@interface WebViewController : UIViewController
- (id)initWithURL:(NSURL *)url frame:(CGRect)frame;
@property (retain) NSURL *url;
@end
在这个例子中,我调用了 WebViewController 类并给它一个自定义的初始化方法。(还给了它一个保存 url 的属性)。
首先在您的实现中,您应该综合该属性
@implementation WebViewController
@synthesize url = _url;
在实现中,您应该执行以下操作来创建您的 init 方法:
- (id)initWithURL:(NSURL *)url frame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.url = url;
}
return self;
}
记住你还需要(如果不使用 ARC):
- (void)dealloc {
self.url = nil;
[super dealloc];
}
那么你还需要有一个:
- (void)loadView {
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:webView];
NSURLRequest *request = [NSURLRequest requestWithURL:self.url];
[webView loadRequest:request];
[webView release]; // remove this line if using ARC
// EDIT :You could add buttons that will be on all the controllers (pages) here
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 addTarget:self action:@selector(buttonTap) forControlEvents: UIControlEventTouchUpInside];
[self.view addSubview:button1];
}
还请记住,您将需要实现该方法
- (void)buttonTap {
// Do something when the button is tapped
}
// END EDIT
在具有 UIPageViewController 的主控制器中,您需要执行以下操作:
NSMutableArray *controllerArray = [NSMutableArray array];
for (NSUInteger i = 0; i < urlArray.count; i++) {
WebViewController *webViewController = [[WebViewController alloc] initWithURL:[urlArray objectAtIndex:i]];
[controllerArray addObject:webViewController];
// EDIT: If you wanted different button on each controller (page) then you could add then here
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 addTarget:self action:@selector(buttonTap) forControlEvents: UIControlEventTouchUpInside];
[webViewController.view addSubview:button1];
// In this case you will need to put the "buttonTap" method on this controller NOT on the webViewController. So that you can handle the buttons differently from each controller.
// END EDIT
[webViewController release]; // remove this if using ARC
}
pageViewController.viewControllers = controllerArray;
因此,我们基本上只是为您要显示的每个页面创建了一个 WebViewController 类的实例,然后将它们全部添加为 UIPageViewController 的视图控制器数组,以便在它们之间进行分页。
假设 urlArray 是一个有效的 NSArray,其中包含您要加载的所有页面的 NSURL 对象,并且您已经创建了一个 UIPageViewController 并将其添加到您的视图层次结构中,那么这应该可以解决问题。
希望这会有所帮助,如果您需要任何澄清或进一步帮助,请告诉我:)