iOS uses the Xib file to create a component as a child control for reuse

Source: Internet
Author: User

In some cases, we find that there are some controls on the interface that can be reused.

If you copy these controls over and over, it's a bit cumbersome. So, just use a xib file, create a view, put the control you want to reuse, put it in a view, and then reference it in the other viewcontroller.

This will be a lot easier.

The first step: Create a singleviewapplication named "Xib as a child control reuse", in the project new File, select Cocoa Touch Class, named "Aaview", click on next,1 as shown:

Figure 1

Step Two: Create a view, new File inside the project, select View, and click next,2. Named "Aaview", click create,3 as shown:

Figure 2

Figure 3

Step three: Select Aaview.xib, click View, change the custom class to "Aaview" (that is, the AAView.h and aaview.m files you just created), as shown in 4. If you want to modify the size of the view, you can set the property size to "Freeform".

? Figure 4

Fourth Step: Add 4 labels to the view and modify the text,5 as shown. Add outlets to the "Xiao Wang" and "21" two labels, and control-drag to the AAView.h file, respectively, named 6:

Figure 5

Figure 6

Fifth step: Now is how to load this view as a child control to other interfaces, open the Viewcontroller.m file, the AAView.h "import in, and then write the following code in the Viewdidload:

1     Aaview *aaview = [[[NSBundle Mainbundle] loadnibnamed:@ "aaview" owner:self Options:nil] Lastobject]; @ " Xiao Ming ";  @ "";  7 [Self.view Addsubview:aaview];            

Run the program, as shown in result 7:

Figure 7

At this point, you can see that the name and age have changed, and successfully completed using the Xib file as a child control. If you feel dissatisfied with the location, you can change the frame property of the created view to change the size and position.

Sometimes you have a lot of complex controls that make up a complex component and then reuse it, which is a good idea.

iOS uses the Xib file to create a component as a child control for reuse

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.