当另一个 viewController 在屏幕上时调用某个 UIViewController 上的方法?
我有一个带有一堆视图控制器的常规应用程序 - 比如说 viewControllerA、viewControllerB 和 viewControllerWeb。
“viewControllerWeb”是一个 UIWebViewDelegate,里面只有一个 UIWebView。即使我与 viewControllerA 或 viewCOntrollerB 交互,我也想调用它的方法。 (像 [webview stringByEvaluatingJavaScriptFromString:]
这样的方法)
所以本质上,我似乎希望“viewControllerWeb”以某种方式在后台打开。有人知道如何完成这样的事情吗?我是否以错误的方式思考这个问题?
希望你们能帮帮我!多谢!
I have a regular app with a bunch of view controllers - let's say viewControllerA, viewControllerB, and viewControllerWeb.
"viewControllerWeb" is a UIWebViewDelegate and just has a UIWebView in it. I would like to call methods on it even when I am interacting with viewControllerA or viewCOntrollerB. (methods like [webview stringByEvaluatingJavaScriptFromString:]
)
So essentially, it seems like I want "viewControllerWeb" to be open in the background somehow. Anyone know how to accomplish something like this? Am I thinking about this the wrong way?
Hope you guys can help me out! Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,如果您想从创建 WebViewController 的 VC 调用这些方法,您可以通过将 webViewController 设置为全局变量来轻松实现。
如果您想从外部 VC 访问它,那么您可以将 WebViewController 设为一个单例,您可以从应用程序内的任何位置访问它。像这样的事情会做:
这将允许您从任何地方访问您的WebViewController:
[[WebViewController共享控制器] stringByEvaluatingJavaScript...]
编辑:正如RyanR在评论中指出的那样,这显然会保留您的视图控制器,直到您明确释放它。单例的常见错误是忘记它们并且永远不释放它们 - 确保你这样做!
Well, if you want to call these methods from the VC where the WebViewController was created, you could easily do that by making webViewController a global variable.
If you want to access it from an outside VC, then you could make WebViewController a singleton that you can access from anywhere inside your app. Something like this would do:
That would allow you to access your WebViewController from anywhere:
[[WebViewController sharedController] stringByEvaluatingJavaScript...]
EDIT: As RyanR pointed out in the comment, this obviously will keep your view controller retained until you explicitely release it. Common mistake with singletons is to forget about them and never release them — make sure you do!
您不应该保留视图控制器的副本。如果您希望共享功能,请将其抽象为单例。
You shouldn't be retaining a copy of your view controller. In case you want functionality to be shared, abstract it out to a singleton.