OC自定义对象的copy

一、概念

1、copy的目的在于修改源对象或者副本的时候互不影响

2、只有在不可变对象copy的时候才是浅拷贝,其余都是深拷贝

二、自定义对象的copy(必须首先遵守NSCopying协议)

1、自定义对象copy的必要步骤

1>遵守NSCopying协议

2>重写实现copyWithZone:方法

2、ARC的copy

实现copyWithZone:(NSZone *)zone

e.g.:person对象有两个属性name和age

- (id)copyWithZone:(NSZone *)zone{

Person *person = [[self alloc] init];

person.age = self.age;

person.name = self.name;

return person;

}

3、MRC的copy

1>做完必要的遵守协议和实现copyWithZone方法

2>对OC对象属性重写其内存管理的setter方法

3>重写dealloc方法

3、zone空间介绍

1>因为copyWithZone:当中的zone就是要为对象开辟的内存空间(OC的alloc就是C语言的malloc开辟内存用的,同样需要告诉系统你要开辟空间大小)

2>NSZone空间分配出来的堆空间是相对比较连续的,可以减少内存碎片,一般程序开发没有必要关心


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 iOS 中,可以使用 Objective-C 自定义 TabBar。下面是一个简单的步骤: 1. 创建一个继承自 UITabBarController 的类,例如 MyTabBarController。 2. 在 MyTabBarController.m 中,实现 viewDidLoad 方法,在其中创建自定义 TabBar。 ```objc - (void)viewDidLoad { [super viewDidLoad]; // 创建自定义 TabBar MyTabBar *myTabBar = [[MyTabBar alloc] init]; myTabBar.delegate = self; [self setValue:myTabBar forKey:@"tabBar"]; } ``` 3. 在 MyTabBarController.m 中,实现 UITabBarDelegate 协议中的方法,例如 tabBar:didSelectItem:,处理 TabBar 点击事件。 ```objc - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item { // 处理 TabBar 点击事件 } ``` 4. 创建一个继承自 UITabBar 的类,例如 MyTabBar。 5. 在 MyTabBar.m 中,实现 layoutSubviews 方法,在其中布局 TabBar。 ```objc - (void)layoutSubviews { [super layoutSubviews]; // 布局 TabBar } ``` 6. 在 MyTabBar.m 中,实现 drawRect: 方法,在其中绘制 TabBar。 ```objc - (void)drawRect:(CGRect)rect { // 绘制 TabBar } ``` 7. 在 MyTabBar.m 中,实现 touchUpInside 方法,在其中处理 TabBar 点击事件。 ```objc - (void)touchUpInside:(UIButton *)button { // 处理 TabBar 点击事件 } ``` 8. 在 MyTabBar.m 中,添加 TabBar 上的按钮,例如: ```objc - (void)layoutSubviews { [super layoutSubviews]; // 添加 TabBar 上的按钮 UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom]; button1.frame = CGRectMake(0, 0, self.frame.size.width / 4, self.frame.size.height); [button1 addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button1]; UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom]; button2.frame = CGRectMake(self.frame.size.width / 4, 0, self.frame.size.width / 4, self.frame.size.height); [button2 addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button2]; UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom]; button3.frame = CGRectMake(self.frame.size.width / 2, 0, self.frame.size.width / 4, self.frame.size.height); [button3 addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button3]; UIButton *button4 = [UIButton buttonWithType:UIButtonTypeCustom]; button4.frame = CGRectMake(self.frame.size.width * 3 / 4, 0, self.frame.size.width / 4, self.frame.size.height); [button4 addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button4]; } ``` 9. 在 MyTabBar.m 中,重写 setItems:animated: 方法,在其中设置 TabBar 上按钮的图标和标题。 ```objc - (void)setItems:(NSArray<UITabBarItem *> *)items animated:(BOOL)animated { [super setItems:items animated:animated]; // 设置 TabBar 上按钮的图标和标题 for (int i = 0; i < items.count; i++) { UITabBarItem *item = items[i]; UIButton *button = self.subviews[i + 1]; [button setImage:item.image forState:UIControlStateNormal]; [button setTitle:item.title forState:UIControlStateNormal]; [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; } } ``` 以上就是使用 Objective-C 自定义 TabBar 的简单步骤。当然,还可以根据需求进行更详细的定制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值