問題描述
我正在使用 nib 文件加載視圖:
I am loading a view from a nib file using:
NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:@"AnalysisView" owner:self options:nil];
AnalysisView *gridView = [nibViews objectAtIndex: 0];
nib 包含一個名為 gridScrollView 的滾動視圖,在 AnalysisView 實現文件中,我有一個將視圖作為子視圖添加到滾動視圖的方法:
The nib contains a scrollview called gridScrollView and in the AnalysisView implementation file I have a method which adds views as subviews to the scrollview:
for (NSInteger i = [results count] -1; i >= 0; i--)
{
Result *result = [results objectAtIndex:i];
[self loadResult: result];
}
- (void) loadResult: (Result *) result
{
NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:@"GridView" owner:self options:nil];
GridView *gridView = [nibViews objectAtIndex: 0];
gridView.tag = self.graphCount;
CGRect gridFrame = gridView.frame;
CGFloat yOffset = gridFrame.size.height * self.graphCount;
gridView.frame = CGRectMake(0, yOffset, gridFrame.size.width, gridFrame.size.height);
[self.gridScrollView addSubview: gridView];
self.gridScrollView.contentSize = CGSizeMake(self.gridScrollView.frame.size.width, (yOffset + gridFrame.size.height));
self.graphCount++;
}
我已將滾動視圖委托設置為 AnalysisView 并連接了 do end decelaring 方法
I have set the scrollviews delegate to be AnalysisView and hooked up the did end decelaring method
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSLog(@"%d", [scrollView.subviews count]);
}
當 scrollViewDidEndDecelerating 方法觸發時,它報告子視圖的數量是 5.這些子視圖中的 3 個是我期望的 GridView.但是有 2 個 UIImageViews 我不知道它們為什么在那里.
When the scrollViewDidEndDecelerating method fires it reports that the number of subviews is 5. 3 of these subviews are GridViews which I expect. However there are 2 UIImageViews which I have no idea why they are there.
這是一個問題,因為我打算在 scrollViewDidEndDecelerating 方法中使用 viewWithTag 檢索視圖并在視圖上調用一個方法,但是每當我嘗試檢索標簽為 0 的視圖時,我都會檢索其中一個 UIImageView 和 this導致我的應用程序崩潰,因為無法在圖像視圖上調用該方法.
This is an issue because I intend on retrieving the views with viewWithTag in the scrollViewDidEndDecelerating method and calling a method on the view, however whenever I try to retrieve a view with a tag of 0 I will retrieve one of the UIImageView's and this causes my app to crash because the method cannot be called on an image view.
我知道一種解決方法是將我的 GridView 存儲在一個單獨的實例數組中并從那里引用它們.但我很想知道這 2 個 UIImageView 是什么以及它們是如何到達那里的.
I know a way round this is to store my GridViews in a seperate instance array and reference them from there. But I'm curious to know what the 2 UIImageViews are and how they got there.
推薦答案
UIScrollView
默認包含 2 個 UIImageViews
作為滾動指示器的子視圖.雖然我在文檔中找不到任何關于滾動指示器實現的具體信息,但這些圖像視圖存在于類聲明中(參見 UIScrollView.h
標頭):
UIScrollView
by default contains 2 UIImageViews
as subviews for scroll indicators. Although I can't find anything specific about scroll indicators implementation in docs, those imageviews are present in class declaration (see UIScrollView.h
header):
UIKIT_CLASS_AVAILABLE(2_0) @interface UIScrollView : UIView <NSCoding> {
...
UIImageView* _verticalScrollIndicator;
UIImageView* _horizontalScrollIndicator;
您也可以不從 0 開始分配標簽,而是從某個正數開始 - 這樣可以避免與標準子視圖發生沖突
You can also start assigning tags not from 0, but from some positive number - that way avoiding collision with standard subviews
這篇關于UIScrollView 幻象子視圖的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!