0
votes

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?

1
What I can glean from this is that you have a menu with some url's, probably a tableview, then in the didSelectRow... method you push your view controller? Calling loadRequest on an existing UIWebView will not create a new view controller, so you must be doing that somewhere else. If this is the case, you need to provide a way to access the currently displayed view controller and the UIWebView it has.Matt
@matt I was about to rebuff your comment, but before sounding ignorant I went over my code to make sure I knew what I was talking about. I noticed I was making that call inside a category of my class, ViewController. That might just be the issue. Let me rejig it and see if it works!Ralph Pina
arrg, that did not fix it, it is reloading, but it is doing it by sliding over. I'll need to keep toying with it.Ralph Pina

1 Answers

0
votes

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!