By Xib Custom Controls

Load two ways of xib

  1. NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"test" owner:nil options:nil];
  2. UINib * nib = [UINib nibWithNibName: @ "Test" bundle: nil]; // nil default is mainBundle

    NSArray *array = [nib instantiteWithOwer:nil object:nil];

Xib controller to get in control methods:

  • Through all the child controls
  // 直接遍历子控件设置数据

 for (UIView *view in shopView.subviews) {

        if ([view isKindOfClass:[UIImageView class]]) {

            UIImageView *imageView = (UIImageView *)view;

            imageView.image = [UIImage imageNamed:shop.icon];

        } else if ([view isKindOfClass:[UILabel class]]) {

            UILabel *label = (UILabel *)view;

            label.text = shop.name;

        }

    }
  • Binding tag
// by tag get data corresponding to the sub-control setting 

    the UIImageView * iconImageView = (the UIImageView *) [shopView viewWithTag: . 1 ]; 

    iconImageView.image = [the UIImage the imageNamed: shop.icon]; 

    

    UILabel * NameLabel = (UILabel *) [shopView viewWithTag: 2 ]; 

    nameLabel.text = shop.name;

 

To customize the xib:

  1. Create a ShopView, xib and consistent with its file name (the name taken lightly, it is recommended that makes sense)
  2. In the Xib, xib ShopView changed from class UIView
  3. To the child controls the internal setup data
  4. Class loading procedure provides a method of packaging xib
+ (Instancetype) shopView 

{ 

    return [[[NSBundle mainBundle] loadNibNamed: NSStringFromClass (self) owner: nil options: nil] lastObject]; 

}

 + (Instancetype) shopViewWithShop: (* Shop ) shop 

{ //创建shopView  ShopView

    
* shopView = [self shopView];
 ShopView数据给//设置   shopView.shop
= shop;   return shopView; }

Load the principle of xib

  • Layer by layer corresponding to resolved into code
- (void)loadXib

{

    XMGShopView *shopView = [[XMGShopView alloc] initWithCoder:nil];

    shopView.frame = CGRectMake(0, 0, 70, 90);

    shopView.backgroundColor = [UIColor whiteColor];

    

    UIImageView *iconImageView = [[UIImageView alloc] initWithCoder:nil];

    iconImageView.backgroundColor = [UIColor greenColor];

    iconImageView.frame = CGRectMake(0, 0, 70, 70);

    [shopView addSubview: iconImageView]; 

    self.iconImageView = iconImageView; 

    

    XMGLabel nameLabel = [[XMGLabel alloc] initWithCoder: nil]; 

    nameLabel.backgroundColor = [UIColor greenColor]; 

    nameLabel.frame = CGRectMake ( 0 , 0 , 70 , 70 ); 

    [shopView addSubview: nameLabel]; 

    self.nameLabel = nameLabel;    

}
  • xib controls in what type, load out is what type
  • Xib child control of what type it is, what type of off-line
  • By alloc / init or alloc / initWithFrame create control does not automatically load xib, even if the class name and the name of xib controls as

important point:

  • If the control is created through code, initialization will call initWithFrame
  • If the control is created by xib or storyboard, initialization is not going to call initWithFrame, calls initWithCoder
  • If the control is created by xib or storyboard, after completion of initialization method calls awakeFromNib
  • It recommended initialized in awakeFromNib

Guess you like

Origin www.cnblogs.com/wwjwb/p/12650271.html