Reputation: 189
in my iphone app, im trying to make my uinavigationbar act as a button, i mean do something when i touch it. Not back button o any nav item, the bar itself. heres how i "categorized" it:
@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
UIColor *color = [UIColor blackColor];
UIImage *img = [UIImage imageNamed: @"bar.png"];
[img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
self.tintColor = color;
}
The action would be navigate to some view..
Any ideas?
Thanks in advance!
Upvotes: 0
Views: 709
Reputation: 170319
Do not use categories to override methods from a class. From the Objective-C Programming Language guide:
Although the 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 using this functionality. A category is not a substitute for a subclass. There are several significant shortcomings:
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 already existed 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 since 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 methods may cause behavior changes across all frameworks. For example, if you add an implementation of windowWillClose: to NSObject, this will cause all window delegates to respond to that method and may modify the behavior of all instances of NSWindow instances. This may cause mysterious changes in behavior and can lead to crashes.
Instead, you might be best served by simply creating a transparent view or button that responds to touch and placing that above the bar area for your UINavigationController. This transparent view could then catch touch events intended for the entire bar area.
However, I'd think really hard about what you're trying to achieve with this. Overriding the standard user interactions from a built-in interface element may lead to user frustration and even potential rejection by Apple.
Upvotes: 1
Reputation: 2289
Try overriding these three functions in your category: http://developer.apple.com/iphone/library/documentation/uikit/reference/UIResponder_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006783-CH4-SW13
Upvotes: 0