【发布时间】:2014-10-20 04:01:06
【问题描述】:
从这个网站和其他网站阅读和尝试所有这些不同的解决方案已经快一周了..但不幸的是没有任何效果..
我正在尝试开发一个新闻应用程序(基本上是主页和详细信息页面)..我想在两个视图控制器中使用两个不同的 uiwebviews..例如:FirstView 包括第一个 uiwebview ..和这个 uiwebview显示包含许多不同新闻(链接)的页面(index.htm)..当用户单击任何链接(例如 details.htm?Id=..)时,它会在 SecondView 控制器的第二个 uiwebview 中显示该链接.. .
我真正需要的是,当用户单击 uiwebview1 上的链接时,它会直接打开视图控制器 2(SecondView)并在 webview2 上打开该链接......我知道这是通过使用 shouldStartLoadWithRequest ,因为我见过很多(如果没有所有)如何使用它的例子..但它从来没有对我有用..第二个视图控制器永远无法显示..因为我还设置了委托=自我..
我真的很努力,但因为我是 新的 ios 开发人员 .. 我真的需要一些帮助才能完成这件事...如果有人给我一个,我将不胜感激下载示例的链接,不是因为任何原因,但我有时发现很难将代码放在正确的位置......这可能是我的问题......
非常感谢...
========= 我在Within a (BOOL)webView: How to open a URL clicked in UIWebView that opens a Modal UIWebView找到了这个解决方案
经过大量工作..所有错误都消失了..但仍然无法移动第二个视图控制器..它从未显示..我使用了这段代码: 这是在 viewcontroller.h
@property (nonatomic, strong) IBOutlet UIWebView *webView;
//- (IBAction)prepareForSegue;
@property (strong, nonatomic) NSURL *url;
@property (strong, nonatomic) NSURL *targetUrl;
这是在 viewcontroller.m 上
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
//Gets the link.
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSURL *URL = [request URL];
NSLog(@"url:%@",request); //Get's the url itself
// [self.navigationController pushViewController:SecondView animated:YES];
// [secondView release];
self.webView.delegate = self;
// ViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondView"];
// [self.navigationController pushViewController:controller animated:YES];
//
if ([[URL scheme] isEqualToString:@"http"] ||
[[URL scheme] isEqualToString: @"https" ]) {
targetUrl = url;
[self performSegueWithIdentifier:@"SecondView" sender:self];
return NO;
}
return YES;
}
return YES;
}
在此之前
@synthesize webView;
@synthesize targetUrl;
@synthesize url;
最后:
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSLog(@"Source Controller = %@", [segue sourceViewController]);
NSLog(@"Destination Controller = %@", [segue destinationViewController]);
NSLog(@"Segue Identifier = %@", [segue identifier]);
if ([segue.identifier isEqualToString:@"SecondView"]) {
ViewController *wVC = [segue destinationViewController];
wVC.url = targetUrl ; // replace article.url with your url.
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewDidLoad {
NSURL *urla= [NSURL URLWithString:@"Index.htm"]; NSURLRequest *requestURL = [NSURLRequest requestWithURL:urla]; [webView loadRequest:requestURL];
//
我在我的项目中完全复制了上面的代码..我相信有一个很小的错误...当我单击 index.htm 中的链接时它根本没有显示我的 SecondView...我应该怎么做更改该代码...
非常感谢...
【问题讨论】:
-
你的
prepareForSegue:方法被调用了吗? (也就是说,你看到那些NSLog()的结果了吗?) -
您好,先生,感谢您的回复.. NSLog 中确实没有显示任何内容.. 请您下载我的整个项目并告诉我错误在哪里(Abroadtostudy.com/WebAPP.zip)我把它上传到我自己的网站上......非常感谢你的帮助@PhillipMills
标签: ios objective-c xcode cocoa uiwebview