I'm developing the iPhone app. I custom defined the titleview for navigation bar. The title view contains a button, I also defined the delegate method to support the button click event. But when the button clicked, the delegate cannot be executed. I am not sure why ? Below as my codes: 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;
}
In my ViewController, i implement the protocal as below:
MyViewController.h
@interface MyViewController : UIViewController<UPDelegate>
and in the .m file, i wrote the delegate method, but cannot be exeucte. MyViewController.m
-(void)buttonClick{
NSLog("click title button");
}