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