0

異なるUIWebViewのURLの各ページにどのように表示できるか知りたいUIPageViewControllerのですが、最初のpdfはone.pdf、2番目のpdfはtwo.pdfなどです...

Xcode4.2でUIPageViewControllerを使用しています

4

1 に答える 1

3

これを行う最良の方法は、カスタム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のviewControllerの配列としてページ間で追加しました。

urlArrayがロードするすべてのページのNSURLオブジェクトを含む有効なNSArrayであり、UIPageViewControllerを作成してビュー階層に追加した場合、これでうまくいくはずです。

これがお役に立てば幸いです。説明やサポートが必要な場合は、お知らせください:)

于 2012-01-29T01:32:08.303 に答える