0

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?

4

1 に答える 1

0

わかりました、私はこの振る舞いが起こっている理由を理解しました:

webViewからいくつかのURLをキャプチャし、メソッドで新しいViewControllerをプッシュしています。

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

プッシュするのではなく、リロードしたいだけのURLを適切にチェックしていませんでした。したがって、上記のメソッドが呼び出されるたびに、URLを使用して新しいViewControllerがプッシュされました。

入って、別のビューにプッシュされたくないURLのサブストリングを取得しました。

- (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;
}

次に、サブストリングをキャッチするために上記のメソッドを修正しました。

//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;
}

今はうまくいくようです!

于 2012-10-07T00:00:54.827 に答える