How do I make a custom delegate protocol for a UIView subclass?
- by timothy5216
I'm making some tabs and I want to have my own delegate for them but when I try to send an action to the delegate nothing happens.
I also tried following this tutorial:
link text
But it doesn't work for me :(
Here is my code:
TiMTabBar.h
@protocol TiMTabBarDelegate;
@interface TiMTabBar : UIView {
id<TiMTabBarDelegate> __delegate;
...
int selectedItem;
...
}
//- (id)init;
- (id)initWithDelegate:(id)aDelegate;
- (void)setSelectedIndex:(int)item;
..
@property (nonatomic) int selectedItem;
@property(assign) id <TiMTabBarDelegate> __delegate;
..
...
@end
@protocol TiMTabBarDelegate<NSObject>
//@optional
- (void)tabBar:(TiMTabBar *)_tabBar didSelectIndex:(int)index;
@end
TiMTabBar.m:
#import "TiMTabBar.h"
...
@interface NSObject (TiMTabBarDelegate)
- (void)tabBar:(TiMTabBar *)_tabBar didSelectIndex:(int)index;
@end
@implementation TiMTabBar
@synthesize selectedItem;
@synthesize __delegate;
...
/*
- (id)init
{
...
return self;
}
*/
- (id)initWithDelegate:(id)aDelegate;
{
//[super init];
__delegate = aDelegate;
return self;
}
- (void)awakeFromNib
{
//[self init];
//[self initWithDelegate:self];
...
}
- (void)setSelectedIndex:(int)item {
selectedItem = item;
if (self.__delegate != NULL && [self.__delegate respondsToSelector:@selector(tabBar:didSelectIndex:)]) {
[__delegate tabBar:self didSelectIndex:selectedItem];
}
...
if (item == 0) {
...
} else if (item == 1) {
...
} else if (item == 2) {
...
} else if (item == 3) {
...
} else if (item == 4) {
...
} else {
...
}
}
/*
- (void)tabBar:(TiMTabBar *)_tabBar didSelectIndex:(int)index;
{
//[delegate tabBar:self didSelectIndex:index];
//if (self.delegate != NULL && [self.delegate respondsToSelector:@selector(tabBar:didSelectIndex:)]) {
//[delegate tabBar:self didSelectIndex:selectedItem];
//}
NSLog(@"tabBarDelegate: %d",index);
}
*/
@end
The delegate only works works inside itself and not in any other files like:
@interface XXXController : UIViewController <TiMTabBarDelegate> {
...
...
IBOutlet TiMTabBar *tabBar;
...
}
...
@end
XXXController.m:
#import "XXXController.h"
#import <QuartzCore/QuartzCore.h>
@implementation XXXController
- (void)viewDidLoad {
[super viewDidLoad];
[self becomeFirstResponder];
...
tabBar = [[TiMTabBar alloc] initWithDelegate:self];
//tabBar.__delegate = self;
...
}
#pragma mark TiMTabBar Stuff
- (void)tabBar:(TiMTabBar *)_tabBar didSelectIndex:(int)index;
{
NSLog(@"Controller/tabBarDelegate: %d",index);
}
@end
None of this seems to work in XXXController. Anyone know how to make this work?