gpt4 book ai didi

ios - 无法从另一个 View Controller 在 WebView 中显示 URL

转载 作者:行者123 更新时间:2023-11-28 22:38:44 25 4
gpt4 key购买 nike

我有一个需要打开 WebView 的 UITableViewController。

在我的函数中我有:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

LinkViewController *linkViewController = [[LinkViewController alloc] initWithNibName:@"LinkViewController_iPhone" bundle:nil];

[linkViewController.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]];

[self.navigationController pushViewController:linkViewController animated:YES];

}

我已连接所有 socket ,但没有收到任何警告,但我没有看到页面拉起。

但是,如果我进入实际的 LinkViewController 文件并执行如下操作:

- (void)viewDidLoad
{
[super viewDidLoad];

NSString *urlAddress = @"http://www.google.com";

//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];

//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

//Load the request in the UIWebView.
[self.webView loadRequest:requestObj];
}

一切似乎都很好。我不明白为什么?

最佳答案

您应该将 URL 属性添加到您的 LinkViewController。然后在您的 TableView Controller 中执行此操作:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
LinkViewController *linkViewController = [[LinkViewController alloc] initWithNibName:@"LinkViewController_iPhone" bundle:nil];
linkViewController.URL = [NSURL URLWithString:@"http://www.google.com"];

[self.navigationController pushViewController:linkViewController animated:YES];
}

现在在您的 LinkViewController 中您可以:

- (void)viewDidLoad {
[super viewDidLoad];

//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:self.URL];

//Load the request in the UIWebView.
[self.webView loadRequest:requestObj];
}

你原来的有两个问题:

  1. 在 TableView Controller 中,您在初始化之前访问了 webView 属性。这就是什么都没有发生的原因。
  2. 您向外界公开了太多 LinkViewController 的实现细节。如果您在应用程序的许多地方使用 LinkViewController,您需要该类的每个用户都知道创建一个 URL,创建一个请求,然后告诉内部 Web View 开始加载。所有这些逻辑都属于 LinkViewController。现在类(class)的每个用户只需要知道设置一个简单的 URL 属性。

关于ios - 无法从另一个 View Controller 在 WebView 中显示 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15180498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com