2013-08-08 2 views
2

수정 됨 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 
올바른 방법으로 대리자를 사용하는 방법
+1

이 차례로 그 UIView의 하위 클래스를로드하는 펜촉에서 엮어서 하위보기로 추가합니다. 나는이 권리를보고 있는가? – Justin

+0

나는 objective-c에 새로운데이 서브 뷰를 추가하기위한이 솔루션은 stackoverflow에서 somwhere를 발견하고 그것을 올바르게 보았다. 그런 식으로 작동한다 ... 그리고 "tap tap"은 작동하는 유일한 것이다 ... – user2199890

+0

거의 재귀 적으로 보입니다.하지만 펜촉에서로드 할 때'-initWithFrame'이 호출되는지 모르겠습니다. – Justin

답변

1

왜 그냥하지 :

그것은 당신이 UIView의 서브 클래스를로드하는 나에게 매우 이상한
@implementation ViewController 

- (void)viewDidLoad 
{ 
    CGRect frame = CGRectMake(100, 100, 212, 45)]; 
    NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"AnnotationView" 
                 owner:nil 
                 options:nil]; 
    for (id object in nibContents) { 
     if ([object isKindOfClass:[AnnotationView class]]) { 
      _annot = (AnnotationView *)object; 
     } 
    }   
    _annot.frame = frame; 
    _annot.backgroundColor = [UIColor greenColor]; 
    _annot.textLabel.text = @"some text"; 
    _annot.delegate = self; 
    [self.view addSubview:_annot]; 
} 
+0

이 코드는 '[ setValue : forUndefinedKey :] 오류를 발생시킵니다 :이 클래스는 키 이미지에 대해 코드 호환 키 값이 아닙니다.' – user2199890

+0

'owner :'매개 변수를'self'에서'nil'으로 변경합니다. – Justin

+0

이제 델리게이트가 작동하지만 오류가 계속 나타나기 때문에 내 .xib 파일보기에서 모든 요소를 ​​삭제해야했습니다. 이제이 새로운 문제를 해결할 것입니다. 정말 고마워. – user2199890

1

! SecondViewController.h에서

: SecondViewController.m에서

@protocol messageDelegate <NSObject> 
    @optional 
    -(void) test; 
    @end 
    @interface SecondViewController : NSString 
    @property (nonatomic, assign) id <messageDelegate> delegate; 
    @end 

: ViewController.h에서

-(void)readyToSend 
{ 
    [self.delegate test]; 

} 

:

ViewController.m에서
@interface ViewController : UIViewController<messageDelegate> 
@end 

:

- (void)viewDidLoad { 
SecondViewController *secondViewController = [[SecondViewController alloc] init]; 
secondViewController.delegate = self; 
} 
-(void) test{ 
NSLog(@"Delegate massage is received!"); 
에서

}

희망이 있습니다.

+0

OP가 이미이 작업을 수행합니다. 오류가 어디 있습니까? –

+0

고맙지 만 문제는 아닙니다. 내 대리인은 @ 선택 사항을 제외하고는 귀하의 것과 같습니다. – user2199890

0

viewController는 프로토콜을 구현하는 AnnotationView가 아닙니다. 그래서 당신은 그 줄을 움직일 수 :

@interface AnnotationView : UIView <protName> 
+0

ViewController가 프로토콜을 구현했습니다. –

+0

네, 그게 문제입니다. AnnotationView의 대리자가 호출됩니다. –

+0

죄송합니다. 귀하의 의견을 듣지 못합니다. 내 ViewController 프로토콜을 구현합니다. AnnotationView에 프로토콜을 추가하면 문제가 해결되지 않습니다. – user2199890

관련 문제