標籤:nslog return com Owner tool uicolor for first 資料
故事板控制器:
//// ViewController.m// 03-通過xib自訂商品的View#import "ViewController.h"#import "XMGShopView.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // 載入xib// XMGShopView *shopView = [[[NSBundle mainBundle] loadNibNamed:@"XMGShopView" owner:nil options:nil] firstObject];// XMGShopView *shopView = [[XMGShopView alloc] initWithFrame: CGRectMake(100, 100, 80, 100)]; XMGShopView *shopView = [XMGShopView shopView]; shopView.frame = CGRectMake(100, 100, 80, 100); // 給子控制項設定屬性 /* UIImageView *imageView = [shopView viewWithTag:100]; UILabel *titleLabel = [shopView viewWithTag:200]; imageView.image = [UIImage imageNamed:@"danjianbao"]; titleLabel.text = @"單肩包"; */ [shopView setName:@"單肩包"]; [shopView setIcon:@"danjianbao"]; [self.view addSubview:shopView];}@end
xib對應的類:
//// XMGShopView.h#import <UIKit/UIKit.h>@interface XMGShopView : UIView// 提供set方法- (void)setIcon: (NSString *)icon;- (void)setName: (NSString *)name;// 提供快速建立方法+ (instancetype)shopView;@end
// XMGShopView.m/** xib使用注意事項: 1> 如果一個view從xib中載入,就不能用[xxx alloc] init] 和 [xxx alloc] initWithFrame:]建立 2> 如果一個xib經常被使用,應該提供快速構造類方法 3> 如果一個view從xib中載入: 用代碼添加一些子控制項,得在 initWithCoder: 和 awakeFromNib 建立 4> 如果一個view從xib中載入,會調用initWithCoder: 和 awakeFromNib,不會調用init和initWithFrame:方法 */#import "XMGShopView.h"@interface XMGShopView () @property (weak, nonatomic) IBOutlet UIImageView *iconView; @property (weak, nonatomic) IBOutlet UILabel *titleLabel; /** 測試label */ @property (nonatomic, weak) UILabel *label; /** 毛玻璃 */ @property (nonatomic, weak) UIToolbar *toolBar;@end@implementation XMGShopView/** * 如果View從xib中載入,就不會調用init和initWithFrame:方法 * *//*- (instancetype)init{ if (self = [super init]) { NSLog(@"%s", __func__); } return self;}- (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { NSLog(@"%s", __func__); } return self;} *//*** 如果View從xib中載入,就會調用initWithCoder:方法* 建立子控制項,... 注意: 如果子控制項(UIImageView,UILabel)是從xib中建立,是處於未喚醒狀態*/- (instancetype)initWithCoder:(NSCoder *)aDecoder{ if (self = [super initWithCoder:aDecoder]) { /* UILabel *label = [[UILabel alloc] init]; label.backgroundColor = [UIColor grayColor]; label.text = @"哈哈哈哈哈哈"; [self addSubview:label]; self.label = label; */ NSLog(@"1"); } return self;}#pragma mark - xib的載入原理- (UIView *)loadFormNib{//載入應該返回View。 XMGShopView *shopView = [[XMGShopView alloc] initWithCoder:nil]; shopView.frame = CGRectMake(0, 0, 80, 100); UIImageView *iconView = [[UIImageView alloc] initWithCoder:nil]; iconView.backgroundColor = [UIColor greenColor]; iconView.frame = CGRectMake(0, 0, 80, 80); iconView.tag = 100; [shopView addSubview:iconView]; self.iconView = iconView; UILabel *label = [[UILabel alloc] initWithCoder:nil]; label.backgroundColor = [UIColor orangeColor]; label.tag = 200; [shopView addSubview:label]; self.titleLabel = label; return shopView;}/** * 從xib中喚醒 添加 xib中建立的子控制項 的子控制項 */- (void)awakeFromNib{ // 往imageView上加毛玻璃 UIToolbar *toolBar = [[UIToolbar alloc] init]; [self.iconView addSubview:toolBar]; self.toolBar = toolBar; NSLog(@"2");}#pragma mark - 快速構造方法+ (instancetype)shopView{ return [[[NSBundle mainBundle] loadNibNamed:@"XMGShopView" owner:nil options:nil] firstObject];}#pragma mark - 布局子控制項- (void)layoutSubviews{ [super layoutSubviews]; /* self.label.frame = self.bounds; */ self.toolBar.frame = self.iconView.bounds;}#pragma mark - 設定資料- (void)setIcon:(NSString *)icon{ self.iconView.image = [UIImage imageNamed:icon];}- (void)setName:(NSString *)name{ self.titleLabel.text = name;}@end
ios20--xib2