0

我正在尝试显示用户可以在我的应用程序中切换的网页列表。我已经用文件成功地做到了这一点。但是,我不希望用户能够从这些页面导航。我通过查找发现了如何做到这一点。问题是它只在第一页上执行。当我切换到另一个页面时,即使我回到第一页,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
4

1 回答 1

0

好的,我通过反复试验弄明白了,因为没有人愿意帮助我。我尝试寻找类似问题的答案,但实际上对我没有任何帮助。如果您正在阅读本文并遇到同样的问题,这就是您解决问题的方法。您所要做的就是将 web 委托重置为 nil,然后返回 self ,如下所示:

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

我不知道为什么其他人的代码如此复杂,我只能推测为什么会这样。老实说,我很幸运,但我认为这样做可能会清除缓存,从而使每个页面对程序都是新鲜的。那是其他人在谈论的。然而,他们的方法对我不起作用,要么是因为我缺乏经验,要么是因为它们不适合我的特殊情况。这要简单得多,只要您确保每次加载指定网页之一时都这样做,就可以保证工作。如果您正在阅读本文,欢迎您。如果有人愿意看我的帖子并更详细地解释为什么这样做,请随时这样做!

于 2015-05-28T03:27:39.443 回答