0

I am using separate UIView class which is initiating from awakeFromNib This is the view hierarchy of my custom class in which I have to add custom views to contentView. I am not getting how to add my customViews as subviews to contentView.

View Hierarchy

This is the try I made and I failed In it.

 #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];
}
}
Janmenjaya
  • 4,149
  • 1
  • 23
  • 43
Saheb Singh
  • 555
  • 4
  • 18
  • What are you doing in this class, how you loading this class in your ViewController. Also in loadViewIntoMemory method you are again loading a Nib. this method will call awakeFromNib , thus here is a loop. – Dharmendra Chaudhary Sep 08 '16 at 10:42
  • This class will load via awakeFromNib – Saheb Singh Sep 08 '16 at 10:44
  • 1
    Here main problem is you calling loadNibNamed in loadViewIntoMemory which calls awakeFromNib. And awakeFromNib then calls loadViewIntoMemory and so which is a infinite loop. Test this thing placing breakpoint on both your methods then you will know how this code is executing. – Dharmendra Chaudhary Sep 08 '16 at 10:49

2 Answers2

0

Try to write a initializer method, which will return you, your custom UIView loading from .xib file,

- (instancetype)initYourView {
    return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil].firstObject;
}
shuvo
  • 705
  • 5
  • 15
  • What is firstObject? How would i know it is my required view? – Saheb Singh Sep 08 '16 at 10:07
  • If you have confusion, The best practice is to do something like this, + (id)loadNibNamed:(NSString *)nibName ofClass:(Class)objClass { if (nibName && objClass) { NSArray *objects = [[NSBundle mainBundle] loadNibNamed:nibName owner:nil options:nil]; for (id currentObject in objects ){ if ([currentObject isKindOfClass:objClass]) return currentObject; } } return nil; } – shuvo Sep 08 '16 at 10:15
0

I have understand following You have created one .xib Files With three view in that 1. ContentView 2. input textview 3. Input picker view

now you want to added input view and input picker view in your content view.

Now you can do this two ways 1. you create IBOutlet of input text view and input picker view and added subview them in the content view.

  1. You create view object and add subview it in content view like this.

    - (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]]];
    
    }
    

now call this method with custom view object that you have created on ViewController.

Example

ViewController Code in view did load or where you want to add the custom view

 //  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];

Same way you can call custom picker view add methods you need to change index to 2 for picker view

Like,

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

Hope this help you.

Ronak Chaniyara
  • 5,335
  • 3
  • 24
  • 51
user1548843
  • 670
  • 12
  • 20