The usual steps to use for 1.xib
1.1 Create a new 1 class to describe this xib (assuming that it inherits from UIView and the class name is Diyview)
1.2 New 1 Xib, file name is also Diyview, drag a uiview inside, set the UIView property-class for Diyview
1.3 Drag 1 long press gesture recognition to xib, right mouse button just dragged in uiview,gesture recognizer and just dragged in the long press gesture recognition to do the connection
1.4 DIYView.h provides a method class method: + (instancetype) Diyview;
DIYVIEW.M Implementation Diyview:
+ (Instancetype) Diyview
{
return [[[NSBundle Mainbundle] loadnibnamed:@ "Diyview" Owner:nil Options:nil] lastobject];
}
1.5 in the controller, add the view defined by the Xib
Diyview *diy = [Diyview Diyview];
[Self.view Addsubview:diy];
1.6 Last Run program, program crashes
1.7 Analysis of the cause of the error:
[Diyview Diyview] Returns a gesture, not an expected uiview. Because the last addition is a long-press gesture.
1.8 Summary:
When using Xib, pay attention to what lastobject or Firstobject returns.
1.9 Circumvention Methods
You can print an array to see the order of the elements in the array
Nsarray *array = [[NSBundle mainbundle] loadnibnamed:@ "Diyview" Owner:nil Options:nil]
NSLog (@ "%@", array);
Note When using IOS xib