多个选项卡控制单个 UIWebview

发布于 2024-08-11 04:54:02 字数 370 浏览 8 评论 0原文

如何使用多个 UITabBar 选项卡来控制单个 UIWebView?

一个例子:

Pressing tab1: UIWebView loads index.html
Pressing tab2: the same UIWebView loads customers.html
Pressing tab3: the same UIWebView loads tos.html

我的方法是将 UIWebView 放在 AppDelegate 中,并将每个 ViewController 的 self.webview 设置为该单个 UIWebView。但是,WebView 会显示,但显示为白色并且不加载任何内容。

有什么提示吗?

how do I use multiple UITabBar tabs for controlling a single UIWebView?

An Example:

Pressing tab1: UIWebView loads index.html
Pressing tab2: the same UIWebView loads customers.html
Pressing tab3: the same UIWebView loads tos.html

My approach was to put the UIWebView in the AppDelegate and set self.webview of every ViewController to that single UIWebView. However, the WebView gets displayed, but is white and doesn't load anything.

Any hints?

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

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

发布评论

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

评论(3

2024-08-18 04:54:02

WebView 很昂贵,因此您的方法可能是正确的,但您可能希望首先在每个选项卡视图控制器中创建一个单独的 WebView,然后看看事情是否按您预期的方式工作。开销可能会导致您的应用程序开始接收内存警告,但这至少可以让您了解您的页面是否至少可以在该上下文中正确加载。

另外,您可以发布一些代码来显示如何将 webview 添加到每个视图控制器吗?如果您没有使用 -setFrame 设置 Web 视图的框架,那么一旦您将 Web 视图添加为视图控制器视图的子视图,它将无法正确显示。

WebViews are expensive so your approach is probably right, but you may want to start out with creating a separate webview in each tab view controller and see if things are working as you expect there first. The overhead may start causing your app to start receiving memory warnings, but this would at least allow you to see if your pages will at least load correctly in that context.

Also, can you post some code that shows how you're adding your webview to each view controller? If you are not setting the frame of your webview using -setFrame, once you've added the webview as a subview of your view controller view, it won't show up correctly.

清醇 2024-08-18 04:54:02

您需要 -removeFromSuperview 然后 -addSubview 来移动 webview;仅重新分配给实例变量并不能解决问题。

You need to -removeFromSuperview and then -addSubview to move the webview around; just re-assigning to an instance variable won't do the trick.

垂暮老矣 2024-08-18 04:54:02

标签栏对于您想要做的事情来说确实是错误的模型。如果你想切换内容,你真正想要的是工具栏,因为它意味着以各种方式改变一个位置视图 - TabBar 意味着在多个不相关的视图之间切换,因此当你尝试跨多个选项卡使用相同的视图。

现在,如果您确实有完全不相关的内容,为什么不考虑使用不同的 UIWebView,每个选项卡一个。它们并没有那么重,以至于加载多个实例确实会伤害任何东西,并且选项卡在按下之前不会加载内容,因此它们不会立即全部加载。为了节省内存,您甚至可以在切换选项卡时丢弃视图控制器。但是让每个视图管理自己的 Web 视图更有意义,并且意味着您可以更轻松地缓存内容。

A tab bar is really the wrong model for what you are trying to do. If you want to switch content what you really want is a ToolBar, because that is meant to alter an-place view in various ways - TabBar is meant for switching between multiple unrelated views, and as such you are fighting against it when you try to use the same view across multiple tabs.

Now if you really have totally unrelated content, why not consider just using different UIWebViews, one per tab. They are not so heavy that loading having multiple instances really hurts anything, and tabs do not load content until pressed so they will not all load at once. To conserve memory you could even toss away view controllers when tabs are switched. But having each view manage its own web view makes more sense and means you can keep the content cached much easier.

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