Search code examples
iosxcodestoryboardnibexpandable

UIViewController in storyboard with nib file


Hi can I ask if can I mix the xib with my UIViewController in storyboard? Like they share a controller in their files owner because I'm planning to create a expandable view by using nib as the expandedview and I want to pass a value from nib file to the UIViewController in storyboard. Thanks.


Solution

  • I don't recommend you mix xib and view controller from storyboard and hook them all together. If you want to add a UIView as an extended view to your view controller you can do something like this:

    // Load the first view inside our xib from main bundle
    UIView *view = [[NSBundle mainBundle] loadNibNamed:@"nib_name" owner:self options:nil][0];
    
    // Define new X,Y to our view
    float new_x_position = 0;
    float new_y_position = 400;
    view.frame = CGRectMake(new_x_position, new_y_position, CGRectGetWidth(view.frame), CGRectGetHeight(view.frame));
    
    // UILabel inside our nib, '111' is the tag we set to our UILabel
    UILabel *label = (UILabel*)[view viewWithTag:111];
    
    // Add our view to the current view as a sub view
    [self.view addSubview:view];
    

    I hope I understood you correctly.