为什么第一次只调用shouldStartLoadWithRequest?

时间:2015-05-27 22:13:28

标签: uiwebview uiwebviewdelegate restrict shouldstartload

我正在尝试显示用户可以在我的应用中切换的网页列表。我已成功完成了文档。但是,我不希望用户能够从这些页面导航。通过查找我发现了如何做到这一点。问题是它只在第一页上执行。当我切换到另一个页面时,即使我回到第一页,shouldStartLoadWithRequest也会停止工作。 (这都是通过我专门设计的后退和前进按钮完成的。)如何解决这个问题,以便每次调用它并阻止用户点击链接并导航离开我的设置页面?我是Objective-c的新手。所有相关代码如下。有些东西在.h中全球宣布。对不起,这有点粗糙。我只选择了使其成为代码格式化所需的内容。谢谢!

我的.h文件:

#import <UIKit/UIKit.h>

@interface learnViewController : UIViewController <UINavigationControllerDelegate>
{
    IBOutlet UIWebView *web;
    UIButton *backButton;
    UIButton *forButton;
}
@end

我的.m文件

#import "learnViewController.h"
#import "ViewController.h"

static NSString* links[] =
{
    @"http://www.nlm.nih.gov/medlineplus/ency/article/000141.htm",
    @"http://www.nlm.nih.gov/medlineplus/ency/article/000065.htm",
    @"http://www.nlm.nih.gov/medlineplus/ency/article/001087.htm",
    @"http://www.nlm.nih.gov/medlineplus/ency/article/000091.htm",
    @"http://www.nlm.nih.gov/medlineplus/ency/article/007270.htm",
    @"http://www.nlm.nih.gov/medlineplus/ency/article/000145.htm",
    @"http://www.nlm.nih.gov/medlineplus/ency/article/000093.htm",
    @"http://www.nlm.nih.gov/medlineplus/ency/article/000087.htm",
    @"http://www.nlm.nih.gov/medlineplus/ency/article/000140.htm",
    @"http://www.nlm.nih.gov/medlineplus/ency/article/000132.htm"
};
int numlinks = 10;
int i = 0;

@interface learnViewController ()

@end

@implementation learnViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    float viewWidth = self.view.frame.size.width;
    float viewHeight = self.view.frame.size.height;

    self.view.multipleTouchEnabled = true;
    //Background
    UIImageView *backgroundView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, viewWidth, viewHeight)];
    backgroundView.image = [UIImage imageNamed:@"Background.png"];
    [self.view addSubview:backgroundView];
    //Show Portal
    web = [[UIWebView alloc]initWithFrame:CGRectMake(0.0, viewHeight/34.0, viewWidth, (viewHeight - viewHeight/6.4))];
    self->web.delegate = self;
    web.scrollView.scrollEnabled = TRUE;
    web.scalesPageToFit = TRUE;
    [self loadDocument:links[i] inView:web];
    [self.view addSubview:web];
    //Buttons
    UIButton *homeButton = [[UIButton alloc] initWithFrame:CGRectMake((viewWidth/2.0 - viewWidth/6.0/2.0), (viewHeight - viewHeight*280.0/2208.0/2.0 - viewWidth/6.0/2.0), (viewWidth/6.0), (viewWidth/6.0))];
    [homeButton
     setBackgroundImage:[UIImage imageNamed:@"Home.png"]
     forState:UIControlStateNormal];
    [homeButton addTarget:self action:@selector(homePressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:homeButton];

    backButton = [[UIButton alloc] initWithFrame:CGRectMake((viewWidth/2.0/3.5 - viewWidth/9.0/2.0), (viewHeight - viewHeight*280.0/2208.0/2.0 - viewWidth/9.0/2.0), (viewWidth/9.0), (viewWidth/9.0))];
    [backButton
     setBackgroundImage:[UIImage imageNamed:@"Back.png"]
     forState:UIControlStateNormal];
    backButton.enabled = FALSE;
    [backButton addTarget:self
                   action:@selector(backPressed:)
         forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:backButton];

    forButton = [[UIButton alloc] initWithFrame:CGRectMake((viewWidth/2.0/3.5*2.0 - viewWidth/9.0/2.0), (viewHeight - viewHeight*280.0/2208.0/2.0 - viewWidth/9.0/2.0), (viewWidth/9.0), (viewWidth/9.0))];
    [forButton
     setBackgroundImage:[UIImage imageNamed:@"Forward.png"]
     forState:UIControlStateNormal];
    [forButton addTarget:self
                  action:@selector(forPressed:)
        forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:forButton];

    UIButton *refButton = [[UIButton alloc] initWithFrame:CGRectMake((viewWidth - viewWidth/2.0/3.5*2.0 - viewWidth/9.0/2.0), (viewHeight - viewHeight*280.0/2208.0/2.0 - viewWidth/9.0/2.0), (viewWidth/9.0), (viewWidth/9.0))];
    [refButton
     setBackgroundImage:[UIImage imageNamed:@"Scale.png"]
     forState:UIControlStateNormal];
    [refButton addTarget:self
                  action:@selector(refPressed:)
        forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:refButton];

    UIButton *webButton = [[UIButton alloc] initWithFrame:CGRectMake((viewWidth - viewWidth/2.0/3.5 - viewWidth/9.0/2.0), (viewHeight - viewHeight*280.0/2208.0/2.0 -     viewWidth/9.0/2.0), (viewWidth/9.0), (viewWidth/9.0))];
    [webButton
     setBackgroundImage:[UIImage imageNamed:@"Web.png"]
     forState:UIControlStateNormal];
    [webButton addTarget:self
                  action:@selector(webPressed:)
        forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:webButton];
}
//Button presses
-(void)homePressed:(id)sender
{
    ViewController *home = [[ViewController alloc] initWithNibName:nil bundle:nil];
    [self presentViewController:home animated:YES completion:NULL];
}

- (void) refPressed:(id)sender
{
    [self loadDocument:links[i] inView:web];
}

-(void)backPressed:(id)sender
{
    float viewWidth = self.view.frame.size.width;
    float viewHeight = self.view.frame.size.height;
    if (i > 0)
    {
        [web removeFromSuperview];
        i--;
        web = [[UIWebView alloc] initWithFrame:CGRectMake(0.0, viewHeight/34.0, viewWidth, (viewHeight - viewHeight/6.4))];
        [self loadDocument:links[i] inView:web];
        web.scrollView.scrollEnabled = TRUE;
        web.scalesPageToFit = TRUE;
        [self.view addSubview:web];
        forButton.enabled = TRUE;
    }
    if (i == 0)
    {
        backButton.enabled = FALSE;
    }
}

-(void)forPressed:(id)sender
{
    float viewWidth = self.view.frame.size.width;
    float viewHeight = self.view.frame.size.height;
    if (i < numlinks - 1)
    {
        [web removeFromSuperview];
        i++;
        web = [[UIWebView alloc] initWithFrame:CGRectMake(0.0, viewHeight/34.0, viewWidth, (viewHeight - viewHeight/6.4))];
        [self loadDocument:links[i] inView:web];
        web.scrollView.scrollEnabled = TRUE;
        web.scalesPageToFit = TRUE;
        [self.view addSubview:web];
        backButton.enabled = TRUE;
    }
    if (i == numlinks - 1)
    {
        forButton.enabled = FALSE;

    }
}

-(void)loadDocument:(NSString*)documentName inView:(UIWebView*)webView
{
    NSString *url = links[i];
    NSURL *nsurl = [NSURL URLWithString:url];
    NSURLRequest *nsrequest = [NSURLRequest requestWithURL:nsurl];
    [webView loadRequest:nsrequest];
}

-(void)webPressed:(id)sender
{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:links[i]]];
}

-(BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSURL *url = request.URL;
    NSString *urlString = url.absoluteString;
    NSRange range = [urlString rangeOfString:links[i]];
    if (range.location != NSNotFound)
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

- (void)viewDidUnload
{
    [self->web stopLoading];
    self->web.delegate = nil;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

1 个答案:

答案 0 :(得分:0)

好吧,我通过反复试验弄明白,因为没有人想帮助我。我试着寻找类似问题的答案,但实际上并没有对我有用。如果你正在阅读并遇到同样的问题,这就是你解决它的方法。您所要做的就是将Web委托重置为nil,然后重新设置为self:

self.webView.delegate = nil;
self.webView.delegate = self;

我不知道为什么其他人的代码都是如此复杂,我只能推测其原因。老实说我真的很幸运,但我认为这样做可能会清除缓存,从而使每个页面都清晰可见。这是其他人在谈论的内容。然而,他们的方法对我来说不起作用,因为我缺乏经验,或者因为他们不符合我的具体情况。这更简单,只要您确保每次加载指定的网页时都这样做,就可以保证工作。如果您正在阅读本文,欢迎您。如果有人愿意看我的帖子并解释为什么这个更好的细节,请随意这样做!