IOS-xib (nib) Reuse (in some cases, it helps accelerate the implementation of project functions)

Source: Internet
Author: User

0. preface during project development, we often encounter that the basic space in some View is the same, but the data is different. In this case, we can use xib to encapsulate this View for multiple reuse, (For example, TableViewCell with fixed size controls) This can speed up the development progress of our project as much as possible. 1. The basic usage of xib is the File's owner in A. xib, which indicates that the control in xib can be connected to the attributes and methods of the ViewController class. Although it can be connected to the methods and attributes in this ViewController class, it does not mean that the methods in this class can be directly called. You must perform another operation. B. When loading xib, You need to input an owner object to call the method in this object. If you do not input own, a wild pointer error will be reported. Design xib. It is best not to bind the File's owner. Try to make the code less coupled, that is, the association is not close. 2. xib reuse 2. 1. create an xib 2. 2. create a RowView class that encapsulates the property rowView of xib. h copy code 1 # import <UIKit/UIKit. h> 2 3 @ interface RowView: UIView4 + (id) rowViewWithIcon :( NSString *) icon name :( NSString *) name; 5 6 7 @ property (nonatomic, weak) IBOutlet UIButton * iconBtn; 8 9 @ property (nonatomic, weak) IBOutlet UILabel * nameLabel; copy the rowView code. m copy code 1 # import "RowView. h "2 3 @ implementation RowView 4 5 + (id) rowViewWithIcon :( NSString *) icon name :( NSString *) name 6 {7 RowView * view = [[NSBundle mainBundle] loadNibNamed: @ "RowView" owner: nil options: nil] [0]; 8 9 // 1. set the Icon 10 // UIButton * iconBtn = (UIButton *) [view viewWithTag: 1]; 11 [view. iconBtn setImage: [UIImage imageNamed: icon] forState: UIControlStateNormal]; 12 13 // 2. set Name 14 // UILabel * nameLabel = (UILabel *) [view viewWithTag: 2]; 15 view. nameLabel. text = name; 16 17 return view; 18} copy Code 2. 3. in the Controller ViewController class, call the class to generate xib and reuse the copy Code # import "ViewController. h "# import" RowView. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; RowView * row = [RowView rowViewWithIcon: @" 017.png" name: @ "HAHAHA"]; [self. view addSubview: row];} copy the code by passing parameters to generate xib with different data types

Related Article

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.