수정 됨 UIView
인 .xib 파일이 있습니다. UIView
에는 일부 요소 및 탭 제스처 인식기가 포함되어 있습니다.대리자 메서드가 호출되지 않음 (없음)
콘센트가있는 AnnotationView
에 연결됩니다. AnnotationView
에서 탭 이벤트를 잡아 내고 있으며 정상적으로 작동합니다.
그런 다음 부모보기 (ViewController
)에이 이벤트를 전달하려고합니다. AnnotationView
인스턴스가 생성되어 하위보기로 추가되었습니다. 하지만 내 대리자 메서드가 호출되지 않습니다. 이 문제를 해결하는 방법을 모르겠다. 하위 뷰를 사용하고 있거나 대리자가 잘못되어 있기 때문에이 문제가 있는지 알 수 없다. 여기
AnnotationView.h
#import <UIKit/UIKit.h>
@protocol protName <NSObject>
-(void) test;
@end
@interface AnnotationView : UIView
@property (strong, nonatomic) IBOutlet UIImageView *image;
@property (weak, nonatomic) IBOutlet UILabel *textLabel;
@property (weak, nonatomic) IBOutlet UILabel *accessoryLabel;
@property (strong, nonatomic) IBOutlet UITapGestureRecognizer *tapRecognizer;
@property (nonatomic, weak) id <protName> delegate;
-(IBAction)handleTap:(UITapGestureRecognizer *)tapRecognizer;
@end
AnnotationView.m
#import "AnnotationView.h"
@implementation AnnotationView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"AnnotationView"
owner:self
options:nil];
AnnotationView *_myView = [nibContents objectAtIndex:0];
_myView.backgroundColor = [UIColor greenColor];
[self addSubview: _myView];
}
return self;
}
-(IBAction)handleTap:(UITapGestureRecognizer *)tapRecognizer{
NSLog(@"tap tap");
[self.delegate test]; //this delegate is nil
}
@end
ViewController.h
#import <UIKit/UIKit.h>
#import "AnnotationView.h"
@interface ViewController : UIViewController <protName>
@property (nonatomic,retain) AnnotationView *annot;
-(void) test;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController()
@end
@implementation ViewController
- (void)viewDidLoad
{
_annot = [[AnnotationView alloc] initWithFrame:CGRectMake(100, 100, 212, 45)];
_annot.textLabel.text = @"some text";
_annot.delegate = self;
[self.view addSubview:_annot];
}
-(void) viewWillAppear:(BOOL)animated{
_annot.delegate = self;
}
-(void) test{
NSLog(@"Delegate massage is received!");
}
@end
올바른 방법으로 대리자를 사용하는 방법
이 차례로 그 UIView의 하위 클래스를로드하는 펜촉에서 엮어서 하위보기로 추가합니다. 나는이 권리를보고 있는가? – Justin
나는 objective-c에 새로운데이 서브 뷰를 추가하기위한이 솔루션은 stackoverflow에서 somwhere를 발견하고 그것을 올바르게 보았다. 그런 식으로 작동한다 ... 그리고 "tap tap"은 작동하는 유일한 것이다 ... – user2199890
거의 재귀 적으로 보입니다.하지만 펜촉에서로드 할 때'-initWithFrame'이 호출되는지 모르겠습니다. – Justin