views:

256

answers:

2

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?

A: 

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.

bpapa
Even if I get rid of it it wont work
timothy5216
A: 

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.

Jason Jenkins
I've tried this twice. I have one using it in a NIB and one in code but neither of them work and they both have the delegate set
timothy5216
A couple of things to check:* Is your setSelectedIndex:item method of your TimTabBar being called as you expect?* If it is, is the [__delegate tabBar:self didSelectIndex:selectedItem] line being executed?* Have you verified that the delegate is set to the object you think it is? (i.e. set a breakpoint and use the debugger)
Jason Jenkins