IOS xib using--xib to represent the local interface

Source: Internet
Author: User

Xib file is a lightweight used to describe the local interface of the file, in the previous article explains the add Xib file for the controller, this section mainly explains the local interface through the Xib file representation.

< a > Create a xib file

Xib file creation is the choice of the user interface inside the empty template created, of course, if you want to create a new welcome interface, you can directly select the Launch screen template, in addition, storyboard file is also in this choice. When you select the empty template, the file suffix created by default is named Xib, which is the Xib file.

< two > xib File Store local view

The Xib file created by default is empty, you can drag the control from the interface builder in the lower right corner of Xcode, the default drag uiview size is fixed and needs to be set in the right sidebar to change its size.

In a xib file, you can add multiple UIView or UIView subclasses, which are stored sequentially in the array, and can be obtained from an array of xib based on the index position when used.

After adding the local interface in the Xib, it can be obtained by code, which takes the path to the application first, then gets the array in the Xib file, and extracts the desired interface according to the index.

  Code

UIView *view = [[NSBundle mainbundle] loadnibnamed:@ "jrtemp" owner:nil options:nil][0 ];

To get all the interfaces in the Xib file you need to call Loadnibnamed: Method, the first parameter is the file name, the second parameter is the owner of the Xib file, and if the current Xib file is specific to the current controller, this parameter can be added to the controller, However, the interface in the Xib can no longer be used in other controllers. If the interface in the Xib file can be displayed in multiple controllers, the owner is set to nil.

  < three > UIView in Package xib

Using Xib to represent a partial view, you can get it from the Xib file where you need it, and if you get a lot of trouble directly from xib every time, usually we encapsulate each small local interface in Xib with the corresponding class and provide a way to get that view object in this class to optimize the code , the steps are as follows.

  1, first create a class to represent the corresponding local interface in the Xib file.

  2. Find the view to associate in Xib, and set its class to the newly created class in the right sidebar to associate the two.

3 , It is convenient to use the small control in xib to connect with the new class and make the Gizmo a property of the class.

4, add a method in the new, from the Xib file to get the local interface, the code is as follows:

Code

+(instancetype) rowviewfromfile{    return [[NSBundle Mainbundle] loadnibnamed:@ "  Jrtemp" owner:nil options:nil][0];}

  5, complete the above steps, the use of the local interface, you can create objects by class way to quickly get the interface and use, the code is as follows:

Code

Rowview *rowview = [Rowview rowviewfromfile];[ Self.view Addsubview:rowview];

Xib file in the first small view is added to the interface, if not set frame, the default coordinates are the origin, the size of the size of xib, where you can set according to their own needs, the effect is as follows:

Jerry Education
Source:http://www.cnblogs.com/jerehedu/
Copyright Notice: The copyright of this article is Yantai Jerry Education Technology Co., Ltd. and the blog Park is shared, welcome reprint, but without the consent of the author must retain this paragraph statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.
Technical Consultation:

IOS xib using--xib to represent the local interface

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.