0

我正在制作一些选项卡,我想为它们设置自己的委托,但是当我尝试向委托发送操作时,什么也没有发生。

我也尝试过遵循本教程: 链接文本

但这对我不起作用:(

这是我的代码: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

委托仅在其内部工作,而不在任何其他文件中工作,例如:

@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

这些似乎都不适用于 XXXController。任何人都知道如何使这项工作?

4

2 回答 2

1

看起来 - 在 awakeFromNib 中,您将委托设置为自身。因此,每当您尝试发送委托消息时,您实际上是在将它们发送到您的 TiMTabBar 实例,而不是 XXXController。

于 2010-04-19T18:22:01.297 回答
1

viewDidLoad在你分配和初始化一个新的 XXXController之后,你TiMTabBar是否将它添加到控制器的视图中?如果不是,则您单击的视图中的选项卡栏可能是从没有设置委托的笔尖加载的选项卡栏。

于 2010-04-19T19:00:14.853 回答