-3

在 UIWebView 1 单击超链接然后打开 UIWebView 2,单击 UIWebView 2 中的超链接然后打开 UIWebView 3,单击 UIWebView 3 中的超链接然后打开 UIWebView 4...。

如何实施?

我现在打开第二个 UIWebView

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
self.otherWebView = [[UIWebView alloc] init];
    numberOfOpenedWebPage ++;
    self.otherWebView.tag = numberOfOpenedWebPage;

    self.otherWebView.delegate = self;
    CGRect frame = self.view.frame;
    self.otherWebView.frame = CGRectMake(frame.size.width, 0.0f, frame.size.width, frame.size.height);
    [self.view addSubview:self.otherWebView];
    [otherWebView loadRequest:request];

因为每一个新的 webview 都需要共享委托方法,所以我将 uiwebview var 定义为 class var。但我不知道如何创造更多shouldStartLoadWithRequest

谢谢

4

1 回答 1

1

您当前代码的问题是您不检查超链接的打开位置。

你应该根据这个伪代码重写代码:

// Declare a NSMutableArray in the .h file to store all UIWebViews that are loaded this way.
// Assume that I can refer to it as self.allWebViews
// Set tag of the first UIWebView to 0, and put it in self.allWebViews

if (self.allWebViews.count <= webView.tag + 1) {
    Initialize a new UIWebView
    Set tag to self.allWebViews.count
    Add it to self.allWebViews
    Subview the UIWebView
    Set it to load NSURLRequest passed in
} else {
    Get the UIWebView from the (webView.tag + 1) position of the self.allWebViews
    Set the UIWebView to load the NSURLRequest passed in
}

// Never load hyperlink in the current web view
return NO;

请注意,UIWebViews 由 NSMutableArray 的强引用保存。我不确定你的程序是如何工作的,但如果你不再使用它,你必须注意这一点才能释放 UIWebView。

于 2012-06-20T04:38:46.613 回答