简体   繁体   中英

How to [UIWebView loadRequest:] without pushing in a new ViewController

The functionality I want is this:

I have a set of URLs that I want to load into an existing UIWebView without pushing in a new ViewController. I just want the webView to reload with no animation or sliding.

Right now, this is my implementation in ViewController:

@synthesize webView;

@synthesize requestObj;

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization

    //I show a spinner while it is loading. This is done in webViewDidStartLoad:
    viewLoading = YES;

    //this is landing URL, so I draw some buttons in viewDidLoad
    home = YES;  

    //I load this request into my webView in viewDidLoad
    requestObj = [NSURLRequest requestWithURL:[NSURL URLWithString:myURL]];
    }
    return self;
}

Then, somewhere down in the ViewController, if someone presses a specific URL in my sliding menu I call:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:newUrl]];
[webView loadRequest:request];

I cannot figure out how to load this new URL into the exciting webView without a new version of ViewController being pushed into the stack. Every time it calls my initWithNibName: method in ViewController, but I can't figure out a good way to stop it from pushing itself on to the stack, and just update the webView.

I can use [self.navigationController setViewControllers:] to make a new ViewController and keep the controller from drawing the backButton, but it still sliding.

There must be an easy way to reload the webView that I am just not getting. Any suggestions?

Ok, I figured out why this behavior was going on:

I am capturing some URLs from the webView and I am pushing a new ViewController in the method:

BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType

I was not checking properly for the URLs I only wanted reloaded, as opposed to pushed. So everytime the method above was called it pushed a new ViewController with the URL.

I went in and got a substring of the URL I did not want being pushed into another view:

- (BOOL)homeURL:(NSString *)url
{
    BOOL match = NO;
    url = [self urlType:url];

    if ((url) && ((NSOrderedSame == [url compare:subtring])))
    {
        match = YES;
    }

    return match;
}

- (NSString *)urlType:(NSString *)url
{
    NSRange beginSubstring = [url rangeOfString:@"com/"];
    NSString *val = url;

    if (url)
    {
        val = [val substringWithRange:NSMakeRange((beginSubstring.location + 4), substring.length)];
    }

    return val;
}

Then I fixed the method above to catch the substring:

//capture URL
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{

    if (!viewLoading)
    {
        if ([self homeURL:request.URL.absoluteString])
        {
            return YES;
        }
        else
        {
            ViewController *newPage = [[ViewController alloc] initWithNibName:@"ViewController" request:request home:NO bundle:nil];
            [self.navigationController pushViewController:newPage animated:YES];
            return NO;
        }
    }


    return YES;
}

Seems to work well now!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM