Today i needed to add an Integer property to my custom category.
In detail, i need to add the tag property to the UISwipeGestureRecognizer that don’t have it.
How to?
Create your category, with all the properties that you want.
In the category’s implementation you can’t use @syntetize but you need @dynamicto create your custom getter/setter.
Remember to add in your imports runtime.h
[code lang=”java” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]#import
@interface UISwipeGestureRecognizer (Extras)
@property (nonatomic, assign) NSInteger tag;
@end
static char const * const Key = "oKey";
@implementation UISwipeGestureRecognizer (Extras)
@dynamic tag;
– (void) setTag:(NSInteger)tag
{
objc_setAssociatedObject(self, Key, (id)tag, OBJC_ASSOCIATION_ASSIGN);
}
– (NSInteger)tag
{
return (NSInteger)objc_getAssociatedObject(self, Key);
}
@end
[/code]
You can also specify the association policy based on your property:
Associative Object Behaviors
[code lang=”java” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]enum {
OBJC_ASSOCIATION_ASSIGN = 0,
OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1,
OBJC_ASSOCIATION_COPY_NONATOMIC = 3,
OBJC_ASSOCIATION_RETAIN = 01401,
OBJC_ASSOCIATION_COPY = 01403
};[/code]
- OBJC_ASSOCIATION_ASSIGN
Specifies a weak reference to the associated object. - OBJC_ASSOCIATION_RETAIN_NONATOMIC
Specifies a strong reference to the associated object, and that the association is not made atomically. - OBJC_ASSOCIATION_COPY_NONATOMIC
Specifies that the associated object is copied, and that the association is not made atomically. - OBJC_ASSOCIATION_RETAIN
Specifies a strong reference to the associated object, and that the association is made atomically. - OBJC_ASSOCIATION_COPY
Specifies that the associated object is copied, and that the association is made atomically.
that’s all.
Now you can use tag property in UISwipeGestureRecognizer:
[code lang=”java” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]{
_LSwipe = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:@selector(SwipeRecognized:)];_LSwipe.tag = _Btn.tag; // your new tag property
[_MyView addGestureRecognizer:_LSwipe];
}[/code]
You can read more at Apple doc