UICollectionViewController的布局(self.collectionView所有權。collectionViewLayout比self.collectionViewLayout) - UICollectionViewControlle

UICollectionViewController的布局(self.collectionView所有權。collectionViewLayout比self.collectionViewLayout) - UICollectionViewControlle,第1张

I have a UICollectionViewController managing a collection view that uses different layouts for different app states. I'm using -setCollectionViewLayout:animated: to transition between the different layouts. I'm having a bad access error, and it would help immensely to know what actually owns (e.g. keeps a strong reference to) the collection view's current layout.

我有一個UICollectionViewController管理一個集合視圖,它為不同的應用程序狀態使用不同的布局。我使用-setCollectionViewLayout:animated:在不同的布局之間轉換。我有一個錯誤的訪問錯誤,它將極大地幫助我們了解集合視圖的當前布局實際上擁有什么(例如保持對集合視圖的強引用)。

After calling -setCollectionViewLayout:animated:, I've noticed the following (where self is the UICollectionViewController):

在調用-setCollectionViewLayout:animated:之后,我注意到了以下內容(self是UICollectionViewController):

  • self.collectionView.collectionViewLayout returns the new layout.
  • self.collectionView。collectionViewLayout返回新的布局。
  • self.collectionViewLayout still returns the old layout.
  • 自我。collectionViewLayout仍然返回舊布局。

This led me to check the UICollectionView Class Reference, which explains this behavior:

這讓我檢查了UICollectionView類引用,它解釋了這種行為:

collectionViewLayout

The layout object used to initialize the collection view controller. (read-only)

用於初始化集合視圖控制器的布局對象。(只讀)

@property (nonatomic,readonly) UICollectionViewLayout *collectionViewLayout

@ property UICollectionViewLayout * collectionViewLayout(原子只讀)

Discussion
This property contains the layout object you passed to the initWithCollectionViewLayout: method. The layout object in this property is not updated to reflect changes to the collection view itself. You can use this property to refer to the layout object you originally configured the collection view to use.

討論此屬性包含傳遞給initWithCollectionViewLayout:方法的布局對象。此屬性中的布局對象不會更新以反映對集合視圖本身的更改。您可以使用此屬性來引用最初配置的集合視圖的布局對象。

Okay. So self.collectionViewLayout is a weak reference (or is it?) to the initial layout and self.collectionView.collectionViewLayout is a strong reference to the current layout.

好吧。所以自我。collectionViewLayout是對初始布局和self.collectionView的弱引用(還是?)collectionViewLayout強烈引用了當前布局。

To confirm this, I took a dive into the debugger, but wasn't able to find the actual UICollectionView supposedly owned by the UICollectionViewController. Instead, the collection view controller's _view variable was an instance of UICollectionViewControllerWrapperView. Whaaaat?

為了證實這一點,我深入調試了調試器,但是無法找到UICollectionViewController所擁有的實際UICollectionView。相反,集合視圖控制器的_view變量是UICollectionViewControllerWrapperView的實例。Whaaaat嗎?

This whole experience left me with the following questions:

整個經歷給我留下了以下問題:

  1. Where is the actual UICollectionView stored in a UICollectionViewController?
  2. UICollectionViewController中存儲的實際UICollectionView在哪里?
  3. Does self.collectionViewLayout keep a strong reference to the initial layout?
  4. 做自己。collectionViewLayout保留了對初始布局的強烈引用嗎?

2 个解决方案

#1


0  

Where is the actual UICollectionView stored in a UICollectionViewController?

UICollectionViewController中存儲的實際UICollectionView在哪里?

This is really none of your business. I mean, it's interesting that you found a pointer to the collection view, and you could probably find another by searching through the controller's view hierarchy. The important thing is that you should let UICollectionViewController worry about the collection view. I know that's not a satisfying answer when you're trying to figure out why your app is crashing, but aside from debugging it's always a good idea to avoid messing with a view controller's views.

這不關你的事。我的意思是,有趣的是你找到了一個指向集合視圖的指針,你可以通過搜索控制器的視圖層次結構找到另一個。重要的是你應該讓UICollectionViewController擔心集合視圖。我知道,當你試圖弄清楚為什么你的應用程序會崩潰時,這並不是一個令人滿意的答案,但除了調試之外,避免使用視圖控制器的視圖總是一個好主意。

Does self.collectionViewLayout keep a strong reference to the initial layout?

做自己。collectionViewLayout保留了對初始布局的強烈引用嗎?

Judging from the documentation, yes. Objective-C properties are strong by default, and you can see from the property specification that the collectionViewLayout property doesn't specify weak, so it must be strong.

從文件來看,是的。Objective-C屬性默認是強屬性,您可以從屬性規范中看到collectionViewLayout屬性沒有指定弱屬性,所以它必須是強屬性。

Further evidence comes from the documentation you quoted, which basically says that the property provides a pointer to the layout that you provided at initialization. It doesn't say that the property will return nil if you change the layout -- it goes out of the way to say that the property isn't updated and continues to point to the original layout even if you change the layout.

進一步的證據來自您所引用的文檔,它基本上說屬性提供了一個指向您在初始化時提供的布局的指針。它並不是說,如果你改變布局,屬性會返回nil,而是說屬性沒有被更新,即使你改變布局,屬性仍然指向原始布局。

最佳答案:

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复