何时取消 uinavigationcontroller 中的 nsurlconnection

发布于 2024-11-17 21:21:40 字数 725 浏览 2 评论 0 原文

有人可以向我解释一下为什么当我按下 uinavigationcontroller 上的后退按钮时不会调用 viewcontroller dealloc 方法吗?

我想正确取消我的 nsurlconnection,这样它就不会尝试显示弹出窗口并崩溃,因为它的委托不再有效。

我的 NSURLConnection 初始化如下:

NSURLRequest *request = [ [ NSURLRequest alloc ] initWithURL: [ NSURL URLWithString: [ NSString stringWithFormat: @"http://www.url.com" ], string1, string2 ] ] ];

connection = [ [ NSURLConnection alloc ] initWithRequest: request delegate: self ];

[ request release ];

我在 connectionDidFinishLoading 和 didFailWithError 中释放连接,但我想在用户按下后退按钮时取消并释放连接。最合乎逻辑的地方似乎是 dealloc 方法,但我怀疑它不会被调用,因为连接正在保留视图控制器。

确保按下后退按钮时取消连接的最佳方法是什么?我不想使用 viewwilldissapear 因为我以模态方式呈现另一个视图控制器,这不应该成为取消的原因(除非在模态视图控制器中出现相同的问题,弹出+崩溃)。

Can someone please explain to me why the viewcontroller dealloc method doesn't get called when I press the backbutton on a uinavigationcontroller?

I want to properly cancel my nsurlconnection so it doesn't try to display a popup and crash because its delegate is no longer valid.

My NSURLConnection is initialized as follows:

NSURLRequest *request = [ [ NSURLRequest alloc ] initWithURL: [ NSURL URLWithString: [ NSString stringWithFormat: @"http://www.url.com" ], string1, string2 ] ] ];

connection = [ [ NSURLConnection alloc ] initWithRequest: request delegate: self ];

[ request release ];

I release the connection in connectionDidFinishLoading and didFailWithError but I want to cancel and release the connection when the user presses the back button. The most logical place seems to be the dealloc method but I suspect this doesn't get called because the connection is retaining the viewcontroller.

What is the best way to make sure that a connection is cancelled when the back button is pressed? I don't want to use viewwilldissapear because I present another viewcontroller modally which should not be a reason for cancelling (unless the same problem would occur in the modal viewcontroller, pop-up+crash).

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

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

发布评论

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

评论(2

相对绾红妆 2024-11-24 21:21:40

我建议你使用viewwilldisappear。即使您呈现另一个模态视图控制器也没有问题。只要在你的视野中这样做就会消失。然后在 .h 文件中声明你的连接对象

if(urConnection){
  [urconnection Cancel];
  [urconnection release];
}

I suggest you to use viewwilldisappear. Even if you present another modal view controller no problem. And just do like this within ur viewwilldisappear. Declare ur connection object in .h file then

if(urConnection){
  [urconnection Cancel];
  [urconnection release];
}
蹲在坟头点根烟 2024-11-24 21:21:40

好吧,最好在 dealloc 方法或 connectionDidFinishLoading 委托中释放连接对象。但我不认为它用于释放后退按钮上的连接,因为一旦按下后退按钮,对象就会被销毁并调用 dealloc 方法。因此,使用dealloc方法放置释放连接的代码。

Well, it is better to release the connection object in dealloc method or connectionDidFinishLoading delegate. But I dont think its of use to release the connection on back button since once the back button is pressed the object get destroyed itself and calls dealloc method. So, use dealloc method to place code to release connection.

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