代表被送到错课 [英] Delegate Being Sent To Wrong Class

查看:153
本文介绍了代表被送到错课的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将导航栏分类,使标题视图可以点击。当点击时,它将显示另一个视图控制器。我在导航栏中创建一个协议,这将告诉导航控制器已经点击了标题视图。以下是我的导航栏的定义:



NavigationBar.h:

  @protocol NavigationBarDelegate; 

@interface NavigationBar:UINavigationBar
{
id BOOL _titleClicked;
}

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

@end

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

委托实现一个可选方法。 .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];
}

这将创建一个带有徽标的导航栏,并调用当点击标题按钮时,使用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 的实现也在那里。当我点击标题视图时,我收到以下错误: - [UINavigationController titleButtonClicked:]:无法识别的选择器发送到实例



为什么我得到 titleButtonClicked 发送到 UINavigationController ?我的导航控制器有什么需要做的吗?我只是使用简单的旧的 UINavigationController 。我还需要进行子类化吗?如果是这样,为什么?



编辑:



致电 po self.delegate [self.delegate titleViewClicked:_titleClicked]; NavigationBar.m 得到以下结果。代表如何改变其类型?如何解决这个问题?

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


解决方案

您的委托 UINavigationBar 委托属性。重新命名您的代表以消除他们的歧视。


I subclassed my navigation bar, making the title view clickable. When clicked, it will present another view controller. I am creating a protocol in the navigation bar, that will tell the navigation controller that the title view has been clicked. Here is how my navigation bar is defined:

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

The delegate implements one optional method. The .m file is as follows:

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];
}

This creates a navbar with a logo and calls the titleButton method when the title button has been clicked. Everything is fine up till here and the navigation bar displays nicely.

In my 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"];

An implementation of titleButtonClicked is also there. When I click on the title view however, I get the following error: -[UINavigationController titleButtonClicked:]: unrecognized selector sent to instance

Why am I getting titleButtonClicked sent to UINavigationController? Is there something I need to do in my navigation controller? I am just using plain old UINavigationController. Do I need to subclass that too? If so, why?

EDIT:

Calling po self.delegate on line [self.delegate titleViewClicked:_titleClicked]; in NavigationBar.m yields the result below. How did the delegate change its type? How can I fix that?

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

解决方案

You have a clash/ambiguity between your delegate and UINavigationBar's delegate property. Rename your delegate to disambiguate them.

这篇关于代表被送到错课的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆