2016-09-08 39 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]; 

希望这对你有所帮助。