iOS 类别和继承的区别之synthesize not allowed in a category\'s implementation
来源:互联网 发布:linux装进u盘 编辑:程序博客网 时间:2024/06/05 09:55
#import <UIKit/UIKit.h>
@interface UIScrollView (closeCombox)
@property(nonatomic,assign)BOOL haveCombox;
-(void)closeAllTheComBoxView;
@end
#import "UIScrollView+closeCombox.h"
#import "LMComBoxView.h"
@implementation UIScrollView (closeCombox)//synthesize not allowed in a category's implementation 继承父类可以扩展属性
//@synthesize haveCombox;
//此处若在initWithFrame方法里面添加通告,会导致整个程序非常的卡顿!!!
-(void)closeAllTheComBoxView
{
for(UIView *subView in self.subviews)
{
if([subView isKindOfClass:[LMComBoxView class]])
{
LMComBoxView *combox = (LMComBoxView *)subView;
if(combox.isOpen)
{
[UIView animateWithDuration:0.3 animations:^{
CGRect frame = combox.listTable.frame;
frame.size.height = 0;
[combox.listTable setFrame:frame];
[combox.listTable removeFromSuperview];
} completion:^(BOOL finished){
combox.isOpen = NO;
combox.arrow.transform = CGAffineTransformRotate(combox.arrow.transform, DEGREES_TO_RADIANS(180));
}];
}
}
}
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if(self.haveCombox)
{
[self closeAllTheComBoxView];
}
}
@end
但是发现会报错:synthesize not allowed in a category's implementation
尝试自己写set和get方法也不行。
ps:还有一个问题,如果在类别的初始化方法里面添加了一些代码,比如加入我上面这样写:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(closeAllTheComBoxView) name:@"save" object:nil];
}
return self;
}
会导致整个程序非常的卡顿,程序中用到的第三方的下拉加载上拉刷新的UITableView也会非常非常的卡顿,会感觉拖不动,而且UITableView的下拉加载的视图也会漂浮起来。(当时把我吓坏了!)这就是使用类别不当导致的严重后果!
后来我就果断用继承了,我写了一个LMContainsLMComboxScrollView : UIScrollView类,就是包含了LMComboxView的UIScrollView,并在它里面实现触摸收起界面上的所有展开了的下拉列表。
======================================================================
回想一下iOS里面的类别和继承的区别才知道自己犯错了。之前只是看了下概念性的区别,真正自己写代码中遇到了,才能真正体会到它们的区别。
下面是类别和继承的一点区别:
1.Category (分类)可以拓展这个类.添加额外的方法.
Category(分类)使得在不修改该类原先代码的情况下.拓展或者修改现有类的定义.并且是向下有效的.既会影响到该类所有子类.
重写一个类的方式用继承还是分类.取决于具体情况.假如目标类有许多的子类.我们需要拓展这个类又不希望影响到原有的代码.继承后比较好.
如果仅仅是拓展方法.分类更好.(不需要涉及到原先的代码)
2.分类:用来扩展类的方法,不能定义新成员,但是可以访问到私有成员
子类:可以通过覆盖和定义新方法来扩展父类,可以新增成员,但是不能访问父类的私有成员。
我就是没有理解上面两点区别才导致这种错误。在类别中重写了初始化方法,所有影响了所有UIScrollView的子类,导致了程序非常的卡顿。另外分类只能扩展类的方法,不能定义新的成员。
- iOS 类别和继承的区别之synthesize not allowed in a category\'s implementation
- iOS 类别和继承的区别之synthesize not allowed in a category's implementation
- ios类别和继承的区别
- iOS之 @synthesize和@dynamic的区别
- Category(类别)和扩展(Extension)的区别
- 类别(category)和继承
- 类别和继承的区别
- 继承和类别的区别
- iOS 类别 (category)和 类扩展 (extension)的区别
- ios category类别的使用
- ios category类别的使用
- iOS开发中@interface 和 @implementation区别, @property 和@synthesize区别
- ios的类别(category)和扩展(Extension)
- OC中类别和继承的区别
- iOS类别和扩展(Category和Extension)
- protocol、category和继承的区别
- **IOS开发之关键字category(类别)详解**
- IOS中的类别(Category)和扩展(Extension)
- HDOJ 题目1509 Be the Winner(anti-nim)
- iOS开发---网址收藏
- HTML前端ajax请求
- Xcode的Architectures、Valid Architectures和Build Active Architecture Only属性
- Single Round Match 517 Round 1 - Division I, Level Two AdjacentSwaps
- iOS 类别和继承的区别之synthesize not allowed in a category\'s implementation
- iOS UDID(转)
- Android 实现个性的ViewPager切换动画 实战PageTransformer(兼容Android3.0以下)
- 支付宝SDK和ZBar SDK冲突问题
- Xcode6 模拟器不显示键盘解决方案
- Jenkins权限配置
- 使用UITextView的dataDetectorTypes实现超链接需要注意的事项!
- TOP域名:发展分析
- hibernate批量上传