2016-09-08 50 views
0

我使用單獨的UIView類,這是從awakeFromNib開始這是我的自定義類的視圖層次 中,我有自定義視圖添加到contentView(AwakeFromNib)。我沒有得到如何將我的customView添加爲contentView的子視圖。如何添加UIViews到我的內容查看

View Hierarchy

這就是我所做的嘗試,我失敗了。

#pragma mark - UINibLoading 
-(void)awakeFromNib { 
scrollView.translatesAutoresizingMaskIntoConstraints = NO; 
[self loadViewIntoMemory]; 
[self formUpdateDetailsData]; 
} 

#pragma mark - Private 
- (void)loadViewIntoMemory { 
[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil]; 
[self addSubview:contentView]; 
} 

- (void)formUpdateDetailsData { 
for (int i = 1; i < 5; i++) { 
    inputTextView = [[UIView alloc] initWithFrame:CGRectMake(15, ((i*44)+(i*15)), inputTextView.frame.size.width, 44)]; 
    [contentView addSubview:inputTextView]; 
} 
for (int i = 5; i < 10; i++) { 
    inputPickerView = [[UIView alloc] initWithFrame:CGRectMake(15, ((i*44)+(i*15)), inputPickerView.frame.size.width, 44)]; 
    [contentView addSubview:inputPickerView]; 
} 
} 
+0

你在做什麼在這個類,你如何加載這個類在你的ViewController。同樣在loadViewIntoMemory方法中,你再次加載一個Nib。這個方法會調用awakeFromNib,因此這裏是一個循環。 –

+0

這個類將通過awakeFromNib加載 –

+1

這裏主要的問題是你在調用awakeFromNib的loadViewIntoMemory中調用loadNibNamed。然後awakeFromNib然後調用loadViewIntoMemory等等,這是一個無限循環。測試這個東西在你的方法上放置斷點,然後你會知道這個代碼是如何執行的。 –

回答

0

嘗試寫一個初始化方法,這將返回,從文件的.xib您自定義的UIView負荷,

- (instancetype)initYourView { 
    return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil].firstObject; 
} 
+0

什麼是firstObject?我如何知道這是我的必需視圖? –

+0

如果你有困惑,最好的做法是做這樣的事情, +(ID)loadNibNamed:(的NSString *)nibName ofClass:(類)objClass { 如果(nibName && objClass){ 的NSArray *對象= [ [NSBundle mainBundle] loadNibNamed:nibName owner:nil options:nil]; for(id currentObject in objects){ if([currentObject isKindOfClass:objClass]) return currentObject; } } return nil; } – shuvo

0

我明白以下你已經創建了一個文件的.xib在三個視圖1. ContentView 2.輸入文本視圖3.輸入選擇器視圖

現在您想要在內容視圖中添加輸入視圖和輸入選擇器視圖。

現在你可以做到這兩點 1.你創建IBOutlet的輸入文本視圖和輸入選擇器視圖,並在內容視圖中添加子視圖。

  1. 您可以創建視圖對象,並像這樣在內容視圖中添加子視圖。

    - (void)loadInputViewInMemory{ 
    
    UIView *inputView =[[[NSBundle mainBundle]loadNibNamed:@"CustomView" owner:nil options:nil]objectAtIndex:1]; 
    [self addSubview:inputView]; 
    
    
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[inputView]|" options:0 metrics:0 views:[NSDictionary dictionaryWithObjectsAndKeys:inputView,@"inputView",nil]]]; 
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[inputView]|" options:0 metrics:0 views:[NSDictionary dictionaryWithObjectsAndKeys:inputView,@"inputView",nil]]]; 
    
    } 
    

現在調用此方法與你的視圖控制器創建定製的視圖對象。

視圖控制器代碼鑑於沒有負載或您要添加的自定義視圖

// You custom view in your case content view. 
CustomView *customView = [[[NSBundle mainBundle]loadNibNamed:@"CustomView" owner:self options:nil]objectAtIndex:0]; 
[self.view addSubview:customView]; 
customView.translatesAutoresizingMaskIntoConstraints = false; 

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[customView]|" options:0 metrics:0 views:[NSDictionary dictionaryWithObjectsAndKeys:customView,@"customView",nil]]]; 
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[customView]|" options:0 metrics:0 views:[NSDictionary dictionaryWithObjectsAndKeys:customView,@"customView",nil]]]; 

//Call Methods from here instead of calling from awake from nib 


[customView loadInputViewInMemory]; 

你可以調用自定義選擇器視圖添加 需要 變化的方法同樣的方式索引爲2選擇器視圖

贊,

UIView *inputView =[[[NSBundle mainBundle]loadNibNamed:@"CustomView" owner:nil options:nil]objectAtIndex:2]; 

希望這對你有所幫助。