为什么shouldStartLoadWithRequest只被第一次调用?



我试图显示用户可以在我的应用程序之间切换的网页列表。我已经成功地用文档做到了这一点。但是,我不希望用户能够从这些页面导航。我查了一下才知道怎么做。问题是它只在第一页执行。当我切换到另一个页面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

好吧,既然没人愿意帮我,我就是通过反复试验才弄明白的。我试着寻找类似问题的答案,但没有一个对我有效。如果你正在阅读这篇文章,并且遇到了同样的问题,这就是你如何解决它的方法。你所要做的就是将web委托重置为nil然后返回self,如下所示:

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

我不知道为什么其他人的代码都这么复杂,我只能推测为什么这样做是可行的。老实说,我只是运气好,但我认为这样做可能会清除缓存,从而使每个页面对程序来说都是新鲜的。这就是其他人谈论的。然而,他们的方法对我不起作用,要么是因为我缺乏经验,要么是因为它们不适合我的具体情况。这是简单得多,并保证工作,只要你确保它这样做,每次你加载一个指定的网页。如果你在读这篇文章,欢迎你。如果有人愿意看我的帖子,并解释为什么这工作在更好的细节,请放心这样做!

最新更新