iphone - 委托(delegate)被派往错误的类(class)

标签 iphone objective-c ios delegates

我将导航栏子类化,使标题 View 可点击。单击时,它将显示另一个 View Controller 。我正在导航栏中创建一个协议(protocol),它将告诉导航 Controller 标题 View 已被单击。这是我的导航栏的定义方式:

NavigationBar.h:

@protocol NavigationBarDelegate;

@interface NavigationBar : UINavigationBar
{
    id <NavigationBarDelegate> delegate;
    BOOL _titleClicked;
}

@property (nonatomic, assign) id <NavigationBarDelegate> delegate;

@end

@protocol NavigationBarDelegate
@optional
- (void)titleButtonClicked:(BOOL)titleClicked;
@end

委托(delegate)实现了一个可选的方法。 .m文件如下:

NavigationBar.m:

@implementation NavigationBar

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _titleClicked = 0;
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    self.tintColor = [UIColor colorWithRed:(111/255.f) green:(158/255.f) blue:(54/255.f) alpha:(255/255.f)];

    UIImage *image = [UIImage imageNamed:@"titlelogo.png"];

    UIButton *titleButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
    titleButton.backgroundColor = [UIColor colorWithPatternImage:image];
    [titleButton addTarget:self action:@selector(titleButton:) forControlEvents:UIControlEventTouchUpInside];

    //        self.navigationController.delegate = self;
    [self.topItem setTitleView:titleButton];

    [super drawRect:rect];
}

- (void)titleButton:(UIButton *)sender
{
    _titleClicked = !_titleClicked;
    [self.delegate titleButtonClicked:_titleClicked];
}

这将创建一个带有 Logo 的导航栏,并在单击标题按钮时调用 titleButton 方法。到目前为止一切都很好,导航栏显示得很好。

在我的 RootViewController 中:

NavigationBar *navigationBar = [[NavigationBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.frame.size.width, 44.0f)];
navigationBar.delegate = self;
[self.navigationController setValue:navigationBar forKey:@"navigationBar"];

titleButtonClicked 的实现也在那里。但是,当我单击标题 View 时,出现以下错误:-[UINavigationController titleButtonClicked:]: unrecognized selector sent to instance

为什么我将 titleButtonClicked 发送到 UINavigationController?我需要在我的导航 Controller 中做些什么吗?我只是使用普通的旧 UINavigationController。我也需要子类化吗?如果是,为什么?

编辑:

NavigationBar.m 中调用 po self.delegate[self.delegate titleViewClicked:_titleClicked]; 会产生以下结果。委托(delegate)如何更改其类型?我该如何解决?

(lldb) po self.delegate
(objc_object *) $1 = 0x07550170 <UINavigationController: 0x7550170>

最佳答案

正如@idz 所说,问题在于您的:

@property (nonatomic, assign) delegete;

难道你没有看到你甚至没有一个很奇怪:

@synthesize delegete;

<罢工>

那是因为UINavigationBar已经定义了一个 delegate正如 idz 所说的变量。

将您的声明更改为:

// use unsafe_unretained in ARC, not assign
@property (nonatomic, unsafe_unretained) myDelegete;

当然

@synthesize myDelegate;

关于iphone - 委托(delegate)被派往错误的类(class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11775613/

相关文章:

iphone - 如何使用子路径使用 NSBundle 获取资源路径

iphone - 如何将我的付费应用程序免费提供给某人

javascript - 将 JavaScript 注入(inject) UIWebView

objective-c - 递归不崩溃

ios - 无法将搜索栏色调颜色更改为在 iOS 8 中透明

ios - 如何修复 "nib but didn' t get a UITableView"error?

ios - 在将此视频保存到 iOS 中的照片库后获取视频大小

ios - dequeueReusableCellWithReuseIdentifier : and cellForItemAtIndexPath: 之间的区别

iphone - 共享 ADD 和 EDIT 屏幕以使用相同的 UIViewController 是一种好习惯吗?

ios - UIPageViewController 中的离屏页面限制