iOS 类别和继承的区别之synthesize not allowed in a category's implementation
2015-03-01 11:43
239 查看
在项目用到了下拉列表,界面上有很多下拉列表,需要点击背景的时候收起下拉列表。下拉列表是放在UIScollView上面的,我就想在UIScollView内部实现收起下拉列表的方法。期初定义了一个UIScrollView的类别,代码如下:
#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 of 'weak' property is only allowed in
- Write operations are not allowed in read-only mode (FlushMode.NEVER) - turn your Session into FlushMode.AUTO or remove 'readOnly
- (xcode5.0 DEV target:iOS5.0) Could not find a storyboard named 'Main' in bundle
- [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify uniq
- [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify uni
- Code Sign error: ad hoc code signing not allowed with SDK 'iOS 9.3'
- Write operations are not allowed in read-only mode (FlushMode.NEVER) - turn your Session into FlushMode.AUTO or remove 'readOnly
- [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify uniq
- VS2005中编译出错:Error 1 error C3872: '0x3000': this character is not allowed in an identifier
- Delphi - 关于错误E2154 Type '%s' needs finalization - not allowed in variant record
- cvc-complex-type.3.2.2: Attribute 'singleton' is not allowed to appear in element 'bean
- (xcode5.0 DEV target:iOS5.0) Could not find a storyboard named 'Main_iPhone.storyboard' in bundle,解决
- VS错误error C3872: '0x3000': this character is not allowed in an identifier
- ng-repeat出现环路输出Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique
- Write operations are not allowed in read-only mode (FlushMode.NEVER) - turn your Session into FlushMode.AUTO or remove 'readOnly
- VS错误error C3872: '0x3000': this character is not allowed in an identifier
- VS错误error C3872: '0x3000': this character is not allowed in an identifier
- VS错误error C3872: '0x3000': this character is not allowed in an identifier
- VS:error C3872: '0xe044': this character is not allowed in an identifier解决方法