<legend id='u3O19'><style id='u3O19'><dir id='u3O19'><q id='u3O19'></q></dir></style></legend>

      1. <tfoot id='u3O19'></tfoot>
        • <bdo id='u3O19'></bdo><ul id='u3O19'></ul>
        <i id='u3O19'><tr id='u3O19'><dt id='u3O19'><q id='u3O19'><span id='u3O19'><b id='u3O19'><form id='u3O19'><ins id='u3O19'></ins><ul id='u3O19'></ul><sub id='u3O19'></sub></form><legend id='u3O19'></legend><bdo id='u3O19'><pre id='u3O19'><center id='u3O19'></center></pre></bdo></b><th id='u3O19'></th></span></q></dt></tr></i><div id='u3O19'><tfoot id='u3O19'></tfoot><dl id='u3O19'><fieldset id='u3O19'></fieldset></dl></div>

        <small id='u3O19'></small><noframes id='u3O19'>

        在 ARC 下将代表设置为零?

        时间:2023-07-28
        • <bdo id='QfJIA'></bdo><ul id='QfJIA'></ul>

              <tbody id='QfJIA'></tbody>

                <tfoot id='QfJIA'></tfoot>
                <legend id='QfJIA'><style id='QfJIA'><dir id='QfJIA'><q id='QfJIA'></q></dir></style></legend>
                <i id='QfJIA'><tr id='QfJIA'><dt id='QfJIA'><q id='QfJIA'><span id='QfJIA'><b id='QfJIA'><form id='QfJIA'><ins id='QfJIA'></ins><ul id='QfJIA'></ul><sub id='QfJIA'></sub></form><legend id='QfJIA'></legend><bdo id='QfJIA'><pre id='QfJIA'><center id='QfJIA'></center></pre></bdo></b><th id='QfJIA'></th></span></q></dt></tr></i><div id='QfJIA'><tfoot id='QfJIA'></tfoot><dl id='QfJIA'><fieldset id='QfJIA'></fieldset></dl></div>

                  <small id='QfJIA'></small><noframes id='QfJIA'>

                1. 本文介绍了在 ARC 下将代表设置为零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用 ARC 编写 iOS 应用程序并针对 iOS 5+.

                  I'm writing iOS apps using ARC and targeting iOS 5+.

                  假设我编写了一个具有委托属性的自定义视图对象.在声明delegate属性时,我将其设为弱引用以避免retain循环,这样当实际的delegate对象(控制器)被销毁时,我的自定义视图也将被销毁,如下:

                  Suppose I write a custom view object that has a delegate property. In declaring the delegate property, I make it a weak reference to avoid a retain cycle, so that when the actual delegate object (the controller) is destroyed, my custom view will also be destroyed, as follows:

                  @interface MyCustomView : UIView
                  
                  @property (nonatomic, weak) id<MyCustomViewDelegate> delegate;
                  
                  @end
                  

                  一切都好.

                  好的,现在我正在编写控制器对象,它引用了两个视图对象:我的自定义视图和 Apple 提供的 UIKit 视图,它们都声明了委托属性,控制器是两者的委托意见.也许它看起来像这样:

                  Ok, so now I'm writing the controller object, and it has references to two view objects: my custom view and an Apple-supplied UIKit view, both of which declare delegate properties, and the controller is the delegate for both views. Maybe it looks something like this:

                  @interface MyViewController : UIViewController <MyCustomViewDelegate, UITableViewDataSource, UITableViewDelegate>
                  
                  @property (nonatomic, strong) MyCustomView *customView;
                  @property (nonatomic, strong) UITableView *tableView;
                  
                  @end
                  
                  @implementation MyViewController
                  
                  - (void)viewDidLoad
                  {
                      self.customView.delegate = self;
                      self.tableView.dataSource = self;
                      self.tableView.delegate = self;
                  }
                  
                  @end
                  

                  我的问题是:我是否需要重写 dealloc 以将一个或两个委托设置为 nil?

                  My question is this: Do I need to override dealloc to set either or both delegates to nil?

                  我的意思是,据我了解,UIKit 视图的委托属性(在本例中为 tableView)实际上并未声明为弱引用,而是声明为 __unsafe_unretained 参考,用于向后兼容非 ARC 版本的 iOS.所以也许我需要写

                  I mean, as I understand it, the delegate property of the UIKit view (in this case, tableView) isn't actually declared to be a weak reference, but rather an __unsafe_unretained reference, for backwards compatibility with non-ARC version of iOS. So maybe I need to write

                  - (void)dealloc
                  {
                      _tableView.dataSource = nil;
                      _tableView.delegate = nil;
                  }
                  

                  现在,如果我必须重写 dealloc,我仍然不必设置 _customView.delegate = nil,对吗?因为它被(我)声明为弱引用,所以它应该在 MyViewController 销毁时自动设置为 nil.

                  Now, if I do have to override dealloc, I still don't have to set _customView.delegate = nil, right? Because that was declared (by me) to be a weak reference, so it should be set to nil automatically upon the destruction of MyViewController.

                  但另一方面,我不针对非 ARC 版本的 iOS,也不打算这样做.所以也许我根本不需要覆盖 dealloc?

                  But on the other hand, I'm not targeting non-ARC versions of iOS, nor do I intend to. So maybe I don't need to override dealloc at all?

                  推荐答案

                  将非弱代理设置为 nil 通常是一个好主意,除非您知道不必这样做.对于 UITableViewUIScrollView,我在以前的 iOS 版本中遇到过崩溃,执行以下步骤(它可能有助于在启用僵尸的情况下运行):

                  Setting non-weak delegates to nil is generally a good idea unless you know you don't have to. For UITableView and UIScrollView, I've experienced crashes on previous iOS versions with the following steps (it may help to run with zombies enabled):

                  1. 滚动速度非常快.
                  2. 按完成或后退按钮或其他任何方式关闭 VC.

                  这似乎是因为滚动动画保留了对视图的引用,因此视图比 VC 寿命更长.发送滚动事件时崩溃.

                  This appears to happen because the scrolling animation is retaining a reference to the view, so the view outlives the VC. It crashes when sending the scroll event.

                  在加载请求时关闭包含 UIWebView 的 VC 后,我还看到崩溃,其中仅将委托设置为 nil 是不够的(我认为解决方法是调用 [webView loadRequest:nil]).

                  I've also seen crashes after dismissing a VC containing a UIWebView while a request is being loaded, where simply setting the delegate to nil was not sufficient (I think the workaround was to call [webView loadRequest:nil]).

                  这篇关于在 ARC 下将代表设置为零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用委托和协议在 2 个 UIViewController 之间传递数据 下一篇:从应用程序委托获取当前视图控制器(模式是可能的)

                  相关文章

                  <i id='qhnx4'><tr id='qhnx4'><dt id='qhnx4'><q id='qhnx4'><span id='qhnx4'><b id='qhnx4'><form id='qhnx4'><ins id='qhnx4'></ins><ul id='qhnx4'></ul><sub id='qhnx4'></sub></form><legend id='qhnx4'></legend><bdo id='qhnx4'><pre id='qhnx4'><center id='qhnx4'></center></pre></bdo></b><th id='qhnx4'></th></span></q></dt></tr></i><div id='qhnx4'><tfoot id='qhnx4'></tfoot><dl id='qhnx4'><fieldset id='qhnx4'></fieldset></dl></div>

                        <bdo id='qhnx4'></bdo><ul id='qhnx4'></ul>
                      <tfoot id='qhnx4'></tfoot><legend id='qhnx4'><style id='qhnx4'><dir id='qhnx4'><q id='qhnx4'></q></dir></style></legend>

                      <small id='qhnx4'></small><noframes id='qhnx4'>