iOS:我想单击选项卡栏项目以始终指向原始 URL

发布于 2024-12-12 11:50:40 字数 133 浏览 3 评论 0原文

我有一个有 4 个选项卡的应用程序。

这些选项卡之一是 UIWebView。它从特定的 URL 开始。我希望拥有它,以便在通过单击链接等在该 Web 视图中导航后,当您再次单击选项卡项时,它会重新加载原始 URL。我怎样才能做到这一点?

I have an app with 4 tabs.

One of these tabs is a UIWebView. It starts at a particular URL. I would like to have it so that after navigating around in this webview through clicking on links and all that, when you click on the tab item again it reloads the original URL. How could I accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原谅过去的我 2024-12-19 11:50:40

在包含 UIWebView 的视图控制器的 viewWillAppear 方法中,始终将 URL 设置为原始 URL。

- (void) viewWillAppear:(BOOL)animated {
     NSURL *url = [NSURL URLWithString:@”http://www.google.com”];
     NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
     [webView loadRequest:requestObj];
}

编辑回复:您的评论:

//Where you set the UITabBarDelegate (maybe in AppDelegate)
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
   if (item == webViewItem) {
      //Navigating to the web view or already showing it
      [webViewController reloadWebView]
   } else if (webViewNeedsReloading) {
      //Navigating to some other view
      [webViewController reloadWebView]
   }
}

我只是进行了检查,因为我是一个优化狂,讨厌运行不必要的代码。话虽这么说,我确信只要这样做:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
   [webViewController reloadWebView]
}

就可以了,并且对性能没有明显的影响。

In the viewWillAppear method of the view controller that contains the UIWebView, always have it set the URL to the original.

- (void) viewWillAppear:(BOOL)animated {
     NSURL *url = [NSURL URLWithString:@”http://www.google.com”];
     NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
     [webView loadRequest:requestObj];
}

Edit Re: Your comments:

//Where you set the UITabBarDelegate (maybe in AppDelegate)
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
   if (item == webViewItem) {
      //Navigating to the web view or already showing it
      [webViewController reloadWebView]
   } else if (webViewNeedsReloading) {
      //Navigating to some other view
      [webViewController reloadWebView]
   }
}

I only put the checks in because I'm an optimizing fiend and hate running code unnecessarily. That being said, I'm sure just doing:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
   [webViewController reloadWebView]
}

would be fine and have no perceivable impact on performance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文