我正在开发 iPhone 应用程序。我自定义了导航栏的标题视图。标题视图包含一个按钮,我还定义了委托方法来支持按钮单击事件。但是当点击按钮时,委托不能被执行。我不知道为什么?下面是我的代码:UPDelegate.h
@protocol UPDelegate <NSObject>
@optional
-(void)buttonClick;
@end
标题视图.h
#import <UIKit/UIKit.h>
#import "UPDelegate.h"
@interface TitleView :UIView
@property (nonatomic, unsafe_unretained) id<UPDelegate> delegate;
-(id)initWithCustomTitleView;
@end
标题视图.m
@synthesize delegate;
-(id)initWithCustomTitleView
{
self = [super init];
if (self) {
UIButton *titleButton = [UIButton buttonWithType:UIBUttonTypeCustom];
titleButton.frame = CGRectMake(0, 0, 20, 44);
[titleButton setTitle:@"ABC" forState:UIControlStateNormal];
// add action
[titleButton addTarget:delegate action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:titleButton];
}
return self;
}
在我的 ViewController 中,我实现了如下协议:
MyViewController.h
@interface MyViewController : UIViewController<UPDelegate>
在 .m 文件中,我编写了委托方法,但无法执行。我的视图控制器.m
-(void)buttonClick{
NSLog("click title button");
}