제목 표시 줄을 클릭 할 수 있도록 탐색 바를 하위 클래스로 만들었습니다. 클릭하면 다른보기 컨트롤러가 표시됩니다. 내비게이션 컨트롤러에 제목보기가 클릭되었음을 알리는 프로토콜이 탐색 바에 생성됩니다. 여기 내 탐색 모음이 정의하는 방법입니다위임 클래스가 잘못된 클래스로 전송되었습니다
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
대리인은 하나의 선택 방법을 구현한다. 다음과 같이 .m
파일은 다음과 같습니다
NavigationBar.m :이 로고가있는 네비게이션 바를 생성하고 타이틀 버튼을 클릭되었을 때 titleButton
메소드를 호출
@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];
}
. 여기까지는 모든 것이 잘되고 탐색 바는 훌륭하게 표시됩니다. 내 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
의 구현이 또한있다. 그러나 제목보기를 클릭하면 다음과 같은 오류 메시지가 나타납니다.
titleButtonClicked
은 UINavigationController
으로 전송되는 이유는 무엇입니까? 내 탐색 컨트롤러에서해야 할 일이 있습니까? 난 그냥 평범한 오래된 UINavigationController
을 사용하고 있습니다. 서브 클래스도 필요합니까? 그렇다면 왜?
편집 :
가 NavigationBar.m
에 라인 [self.delegate titleViewClicked:_titleClicked];
에 po self.delegate
를 호출하면 아래의 결과를 얻을 수 있습니다. 델리게이트는 유형을 어떻게 변경 했습니까? 어떻게 해결할 수 있습니까?
(lldb) po self.delegate
(objc_object *) $1 = 0x07550170 <UINavigationController: 0x7550170>
의 어떤 다른 위임 속성의 이름을 변경하십시오. 자기가 [자기.navigationController setValue : navigationBar forKey : @ "navigationBar"]; 바 대리인을 navController – NeverBe
에 재 할당했습니다. 작동하지 않습니다. – darksky