UIPageViewController - на каждой странице отображается свое веб-представление - PullRequest
0 голосов
/ 27 декабря 2011

Я хотел бы знать, как я могу отобразить на каждой странице UIPageViewController отдельный URL-адрес UIWebView, скажем, что первый pdf - это one.pdf, второй - two.pdf и т. Д ...

Я использую UIPageViewController в Xcode 4.2

1 Ответ

3 голосов
/ 29 января 2012

Лучший способ сделать это - создать собственный подкласс 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 для каждой страницы, которую вы хотите отобразить, а затем добавил их все в виде массива viewControllers для вашего UIPageViewController на страницу между ними.

Предполагая, что urlArray является допустимым NSArray, содержащим NSURLобъекты для всех страниц, которые вы хотите загрузить, и что вы создали UIPageViewController и добавили его в свою иерархию представлений.это должно сработать.

Надеюсь, это поможет, если вам нужны какие-либо разъяснения или дополнительная помощь, дайте мне знать:)

...