Question

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?

Was it helpful?

Solution

In your XXXController viewDidLoad after you alloc and init a new TiMTabBar are you adding it to the controller's view? If not, perhaps the tab bar in your view that you are clicking on is one loaded from the nib that does not have the delegate set.

OTHER TIPS

It looks like - In awakeFromNib, you are setting the delegate to itself. So whenever you try to send the delegate messages, you are actually sending them to your instance of TiMTabBar, not XXXController.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top