如何为 UIView 子类制定自定义委托协议?

发布于 2024-08-29 19:15:34 字数 2888 浏览 8 评论 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 中工作。有人知道如何进行这项工作吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

<逆流佳人身旁 2024-09-05 19:15:34

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

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.

白鸥掠海 2024-09-05 19:15:34

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文