UIView - NSURLConnection 不调用委托

发布于 2025-01-02 23:50:37 字数 227 浏览 3 评论 0原文

我正在使用 Objective-C 类 (UIView)。在该类中,我调用 NSURLConnection (将 NSURLConnectionDelegate 添加到 .h)当我启动连接时,没有任何反应。它没有调用它的方法。是否无法在 UIView 类中调用 NSURLConnection 或者我做错了什么?

谢谢。

I'm using an Objective-C class (UIView). In that class I'm calling an NSURLConnection (added NSURLConnectionDelegate to .h) When I start the connection, nothing happens. It's not calling its methods. Is it just not possible to call NSURLConnection in a UIView class or am I doing something wrong?

Thanks.

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

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

发布评论

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

评论(2

So尛奶瓶 2025-01-09 23:50:37

首先,仅将 添加到对象的接口声明中是不够的,您应该显式地将其设置为 NSURLConnection 的委托。

其次,UIView 绝对是一个错误的候选 NSURLConnection 委托,因为:

  • UIView 是 MVC 的视图部分;
  • 视图可能会被卸载,除非您采取额外措施,例如将其保留在 UIViewController 中。

这使得 UIViewController 成为 NSURLConnection 委托的更好选择。

First of all, it's not enough to add <NSURLConnectionDelegate> to your object's interface declaration, you should explicitly set it as the NSURLConnection's delegate.

Secondly, UIView is definitely a wrong candidate as an NSURLConnection delegate, because:

  • UIView is the view part of MVC;
  • the view may be unloaded unless you take extra measures like retaining it in the UIViewController.

That makes UIViewController a better choice as an NSURLConnection delegate.

这样的小城市 2025-01-09 23:50:37

您应该在 UIViewController 中执行此操作。看看苹果的文档:

UIView 类定义屏幕上的矩形区域以及用于管理该区域中的内容的界面。在运行时,视图对象处理其区域中任何内容的渲染,并处理与该内容的任何交互。[...]

因此 UIView 不是执行与服务器交互之类的操作的正确位置。

You should do this in your UIViewController. Take a look at Apples documentation:

The UIView class defines a rectangular area on the screen and the interfaces for managing the content in that area. At runtime, a view object handles the rendering of any content in its area and also handles any interactions with that content.[…]

So UIView is not the correct place to do stuff like interaction with a server.

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