IOS development practices: xib loading considerations
When xib adds addSubview to the Controller view, the program crashes. Error message:
'Nsinvalidargumentexception ', reason :'-[UITapGestureRecognizer superview]: unrecognized selector sent to instance 0x7ffae1c1b680'
The error is obviously not found.[UITapGestureRecognizer superview] method.
Check the problematic code:
- (void)viewDidLoad { [super viewDidLoad]; ToolBar *toolBar = [ToolBar toolBar]; [self.view addSubview:toolBar];}
# Import "ToolBar. h "@ implementation ToolBar // return xib view + (instancetype) toolBar {NSArray * arr = [[NSBundle mainBundle] loadNibNamed: @" ToolBar "owner: nil options: nil]; return [arr lastObject];} @ end
Looking at the code, we want to return a ToolBar object. How does one return UITapGestureRecognizer ???
In the toolBar method, print the array of the ToolBar in the bundle and find two objects. And the first one is not what we want TooBar.
Let's take a look at xib. There are actually two objects.
Conclusion: [arr lastObject] can be basically determined. The returned result is not what we want. Change to [arr firstObject] to solve the problem. Therefore, in the future, xib will return the array, which is what we want.
Ps: xib objects are arranged in the order they are put in. The order they are viewed is not necessarily the order of objects in the xib file.