Reputation: 3134
how can I apply haptic feedback throughout my app for every touchupinside event of every UIButton without writing code for each individual button? I have tried making a UIButton category and overriding - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
but this messes up some of my UIButton actions, (i may have implemented it badly)
does anyone have any suggestions?
Upvotes: 2
Views: 924
Reputation: 11039
Overriding in categories such kind of methods that mentioned @ChoungTran isn't such good idea. If you want to customize the default methods in a Category, it'd be better to do that in a swizzled method.
But, I'll prefer to make a custom class derived from UIButton, and implement that logic there and use that button everywhere where I need haptic.
From Apple documentation:
Although the Objective-C language currently allows you to use a category to override methods the class inherits, or even methods declared in the class interface, you are strongly discouraged from doing so. A category is not a substitute for a subclass. There are several significant shortcomings to using a category to override methods:
When a category overrides an inherited method, the method in the category can, as usual, invoke the inherited implementation via a message to super. However, if a category overrides a method that exists in the category's class, there is no way to invoke the original implementation.
A category cannot reliably override methods declared in another category of the same class.
This issue is of particular significance because many of the Cocoa classes are implemented using categories. A framework-defined method you try to override may itself have been implemented in a category, and so which implementation takes precedence is not defined.
The very presence of some category methods may cause behavior changes across all frameworks. For example, if you override the windowWillClose: delegate method in a category on NSObject, all window delegates in your program then respond using the category method; the behavior of all your instances of NSWindow may change. Categories you add on a framework class may cause mysterious changes in behavior and lead to crashes.
Upvotes: 3
Reputation: 3441
Try to use category with class UIButton. In UIButton-Extention register event forControlEvents:UIControlEventTouchUpInside
#import "UIButton+Extention.h"
@implementation UIButton (Extention)
- (instancetype)init
{
self = [super init];
if (self)
{
[self addTarget:self action:@selector(customTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
-(void)awakeFromNib
{
[super awakeFromNib];
[self addTarget:self action:@selector(customTouchUpInside:)
forControlEvents:UIControlEventTouchUpInside];
}
- (void)customTouchUpInside:(UIButton *)sender
{
//TODO:
NSLog(@"Do something here...");
}
@end
Updated: Register event when UIButton created programmatically
Upvotes: 1