[英]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. 我有一组URL,我希望将这些URL加载到现有的UIWebView中,而无需推送新的ViewController。 I just want the webView to reload with no animation or sliding. 我只希望webView重新加载而没有动画或滑动。
Right now, this is my implementation in ViewController: 现在,这是我在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: 然后,在ViewController中的某个地方,如果有人在我的滑动菜单中按下了特定的URL,我称之为:
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. 我不知道如何在不将新版本的ViewController推入堆栈的情况下将该新URL加载到令人兴奋的webView中。 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. 每次它在ViewController中调用我的initWithNibName:方法时,但是我想不出一个好方法来阻止它将自身推入堆栈,而只是更新webView。
I can use [self.navigationController setViewControllers:] to make a new ViewController and keep the controller from drawing the backButton, but it still sliding. 我可以使用[self.navigationController setViewControllers:]制作一个新的ViewController并阻止控制器绘制backButton,但它仍会滑动。
There must be an easy way to reload the webView that I am just not getting. 必须有一种简单的方法来重新加载我没有得到的webView。 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: 我正在从webView捕获一些URL,并在方法中推送了一个新的ViewController:
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. 我没有正确检查我只想重新加载而不是推送的URL。 So everytime the method above was called it pushed a new ViewController with the URL. 因此,每次调用上述方法时,它都会使用URL推送一个新的ViewController。
I went in and got a substring of the URL I did not want being pushed into another view: 我进入并获得了我不想被推入另一个视图的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;
}
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! 看起来现在工作良好!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.