我有UIViewController
看起来与此类似。
左侧是带有单元格的TableViewController
,右侧是UIWebView
。单击单元格后,我想根据单击的单元格加载唯一的URL。
出于某种原因,当我点击单元格时,永远不会向UIWebView
发送请求以加载网址。 UIWebView
和UITableViewDelegate
都在同一个UIViewController
。
以下是我的示例代码:
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableURLRequest *request;
if (indexPath.row == 0) {
request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]];
}
else if (indexPath.row == 1) {
request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.yahoo.com/"]];
}
[webView loadRequest:request];
}
为什么这不起作用? UIWebView
中的UITableView
和IBOutlets
都是viewController
。点击被识别,这个webView
加载请求在放入viewDidLoad
函数时有效,但是当点击时使用相同的代码时没有任何事情发生。
我正在使用这个github api,如果它可以帮助你https://github.com/mikefrederick/MFSideMenu
感谢您的帮助, 麦克
答案 0 :(得分:1)
在[webView loadRequest:request];
行后添加此行...
[webView reload];
请参阅此文件...... UIWebView_Class
例如:
[webView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.co.in"]]];
[webView reload];
答案 1 :(得分:1)
问题是我试图让UIWebView在一个不是委托的控制器中加载请求(在这种情况下是侧视图)。相反,我将UIWebView设置为侧视图中的属性,并具有uiWebViewController.webView.loadRequest:请求并且它有效。
感谢大家的建议。
答案 2 :(得分:0)
您是否能够加载WebView
?如果是:
首先,你需要检查xib文件中是否正确地给出了关于Referencing Outlets& amp;委托。
其次,如果连接正确,则执行以下操作:
您已NSMutableURLRequest
取NSURLRequest
而非
见:
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSURLRequest *request;
if (indexPath.row == 0)
{
request =[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]];
}
else if (indexPath.row == 1)
{
request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.yahoo.com"]];
}
[webView loadRequest:request];
}
希望这会有帮助。