2014-06-09 3 views
0

특정보기에 도구 모음을 추가하고 있지만이를 추가 할 수 있도록보기를 전달할 수 있도록 GeneralFunctions에 메서드가 있습니다. 이 버튼을 누르면iOS IBAction 전역 메서드 추가

+ (void)addCommentsBar:(UIView *)view 
{ 
    CGPoint origin = CGPointMake(0.0, view.frame.size.height - 50); 

    UIView *commentsView = [[UIView alloc] initWithFrame:CGRectMake(origin.x, origin.y, 320, 50)]; 
    commentsView.backgroundColor = [PitcheroColours pitcheroBlue]; 

    UILabel *commentsTitle = [[UILabel alloc] initWithFrame:CGRectMake(15, 2, 200, 50)]; 
    commentsTitle.text = @"1 Comment"; 
    commentsTitle.textColor = [UIColor whiteColor]; 

    UIButton *tapComments = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 50)]; 
    tapComments.backgroundColor = [UIColor clearColor]; 
    [tapComments addTarget:self action:@selector(tapComments:) forControlEvents:UIControlEventTouchUpInside]; 

    [commentsView addSubview:commentsTitle]; 
    [commentsView addSubview:tapComments]; 

    [view addSubview:commentsView]; 
} 

그것은 tapComments를 호출

- (IBAction)tapComments:(id)sender 
{ 
    NSLog(@"Done"); 
} 
GeneralFunctions 내부에 앉아있다

하지만 말 충돌이 :

Uncaught exception: +[GeneralFunctions tapComments:]: unrecognized selector sent to class 0x2d5b7c 

는 내가 멀리에서 선택기를 호출하고 있기 때문에 알고 보기,하지만 이것을 달성하는 가장 좋은 방법은 무엇입니까?!

답변

1

addCommentsBar: 메서드는 인스턴스 메서드가 아닌 클래스 메서드입니다. 이 방법은 클래스 메소드이므로이 당신은 self,

[tapComments addTarget:self action:@selector(tapComments:) forControlEvents:UIControlEventTouchUpInside]; 

으로 대상을 추가하는 것이 그 클래스의 class 아닌 인스턴스로 간주됩니다. 그리고 나서 인스턴스 메서드 인 tapComments:을 선택 자로 전달합니다.

그래서 매개 변수로 target을 전달하거나 tapComments: 메서드를 클래스 메서드로 선언하십시오.

+0

위대한 - 지금은 작동하는 클래스 메서드로 정의! –

0

몇 가지 클래스 (예 : 1 또는 2)에서 액세스하려는 경우 대리인을 사용하는 것이 좋습니다. 그러나 전역 소스에 액세스 할 수있는 클래스가 더 있으면 싱글 톤 패턴을 사용하여 전역으로 만듭니다. 제 생각에 싱글 톤을 사용하는 것이 좋습니다.