【发布时间】:2012-08-23 05:35:22
【问题描述】:
我正在开发 iPhone 应用程序。我自定义了导航栏的标题视图。标题视图包含一个按钮,我还定义了委托方法来支持按钮单击事件。但是当点击按钮时,委托不能被执行。我不知道为什么? 下面作为我的代码: UPDelegate.h
@protocol UPDelegate <NSObject>
@optional
-(void)buttonClick;
@end
TitleView.h
#import <UIKit/UIKit.h>
#import "UPDelegate.h"
@interface TitleView :UIView
@property (nonatomic, unsafe_unretained) id<UPDelegate> delegate;
-(id)initWithCustomTitleView;
@end
TitleView.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 文件中,我编写了委托方法,但无法执行。 MyViewController.m
-(void)buttonClick{
NSLog("click title button");
}
【问题讨论】:
-
你在哪里设置代理?
-
你在哪里将代理“设置”到你的 MyViewController?
标签: iphone ios navigationbar titleview