Upload the Xib file step details and load the xib details

Source: Internet
Author: User

Upload the Xib file step details and load the xib details

1. Directly load the xib file without the. h. m file

NSArray *objs = [[NSBundle mainBundle]loadNibNamed:@"XibView" owner:nil options:nil];UIView *xibView = objs[0];xibView.backgroundColor = [UIColor redColor];[self.view addSubview:xibView];
UINib

// A nib object represents an xib File
// UINib * nib = [UINib nibWithNibName: @ "XibView" bundle: [NSBundle mainBundle];
// Generally, the bundle is passed to nil. The default value is mainbundle.

    UINib *nib = [UINib nibWithNibName:@"XibView" bundle:nil];    NSArray *objs = [nib instantiateWithOwner:nil options:nil];    [self.view addSubview:objs[0]];

2. The. h. m file is required and connected.
Method 1. Use Only One xib
Steps:
1. Create a new view that inherits the UIView name: XibView
2. Create an xib file named XibView.
3. In the xib File, click view (not set File's owner), and set Class on the Right To XibView.
// You can drag the control in xib to. h In XibView.
4. Use this view externally

 NSArray *objs = [[NSBundle mainBundle]loadNibNamed:@"XibView" owner:nil options:nil];    XibView *view = objs.firstObject;       [self.view addSubview:view];

Method 2: Use another xib OtherView file in one xib

1. Same as OtherView
2. Same as OtherView
3. In the xib File, click File owner and select OtherView as the Class on the right.
4. Drag the xib view to the. h file of the OtherView.
5. The used OtherView needs to be initialized.
// InitWithCoder is the entry to the xib File

- (instancetype)initWithCoder:(NSCoder *)coder{    self = [super initWithCoder:coder];    if (self) {        [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];        [self addSubview:self.view];    }    return self;}- (void)layoutSubviews {    [super layoutSubviews];    self.view.frame = self.bounds;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.