xGoPox
xGoPox

Reputation: 684

UIPageViewController - each pages display a different Webview

I would like to know how can I display on each pages of an UIPageViewController a different UIWebView's URL, let say that the first pdf is one.pdf, the second one two.pdf etc...

I'm using the UIPageViewController in Xcode 4.2

Upvotes: 0

Views: 2301

Answers (1)

George Green
George Green

Reputation: 4915

The best way to do this would be to create a custom viewController subclass.

@interface WebViewController : UIViewController

- (id)initWithURL:(NSURL *)url frame:(CGRect)frame;

@property (retain) NSURL *url;

@end

In this example I have called the class WebViewController and given it a custom initialiser method. (Also given it a property to hold the url).

first in you implementation you should synthesise that property

@implementation WebViewController

@synthesize url = _url;

In also in the implementation you should do something like this to create you init method:

- (id)initWithURL:(NSURL *)url frame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.url = url;
    }
    return self;
}

remembering you will also need (if not using ARC):

- (void)dealloc {
    self.url = nil;
    [super dealloc];
}

then you will also need to have a:

- (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];
}

Also remember you will then need to implement the method

- (void)buttonTap {
    // Do something when the button is tapped
}

// END EDIT

In your main controller that has the UIPageViewController you will need to do something along the lines of:

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;

So we basically, just created an instance of your WebViewController class for each page that you want to display, then added them all as the array of viewControllers for your UIPageViewController to page between.

Assuming that urlArray is a valid NSArray containing NSURL objects for all of the pages that you want to load, and that you have create a UIPageViewController and added it to your view hierarchy then this should do the trick.

Hope this helps, if you need any clarification or further help, let me know :)

Upvotes: 3

Related Questions