2013-04-25 5 views
0

저는 태그 기반 응용 프로그램을 작성 중이며 각 탭 (ViewController)에서 동일한 함수를 호출하려고합니다.클래스 메서드를 호출 할 수 없습니다.

나는 다음과 같은 방법으로 그것을 할 노력하고있어 :

#import "optionsMenu.h" 

- (IBAction) optionsButton:(id)sender{ 
    UIView *optionsView = [options showOptions:4]; 
    NSLog(@"options view tag %d", optionsView.tag); 
} 

optionsMenu.h 파일 :

#import <UIKit/UIKit.h> 

@interface optionsMenu : UIView 

- (UIView*) showOptions: (NSInteger) tabNumber; 

@end 

optionsMenu.m 파일 :

@import "optionsMenu.h" 
@implementation optionsMenu 

- (UIView*) showOptions:(NSInteger) tabNumber{ 
    NSLog(@"show options called"); 

    UIView* optionsView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    optionsView.opaque = NO; 
    optionsView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f]; 
    //creating several buttons on optionsView 
    optionsView.tag = 100; 

return optionsView; 

} 

@end 

결과는 내가 결코이다 디버그 메시지를 표시하는 "show options"와 따라서 optionsView.tag은 alwa입니다. ys 0.

내가 뭘 잘못하고 있니?

나는 이것이 아마도 가장 쉽고 어리석은 질문이라는 것을 알고 있지만, 나는 그것을 스스로 해결할 수 없다.

모든 의견을 환영합니다.

+0

나는 Objective-C라고 생각합니다. 다음 번에 적절한 언어로 질문에 태그를 답니다. –

+0

'options'가 제대로 초기화 되었습니까? –

+0

optionsMenu * options;로 막 선언되었습니다. –

답변

3

첫 번째로 주목해야 할 것은 이것이 인스턴스 메소드이며 질문 제목에 설명 된대로 Class 메소드가 아니라는 것입니다. 즉,이 메소드를 호출하려면 클래스의 인스턴스를 alloc/init해야하고 인스턴스에 메시지를 보내야합니다. 예를 들면 : 당신은 그냥 사전 구성된 UIView를 반환하는 클래스의 방법을 만들려면

// Also note here that Class names (by convention) begin with 
// an uppercase letter, so OptionsMenu should be preffered 
optionsMenu *options = [[optionsMenu alloc] init]; 
UIView *optionsView = [options showOptions:4]; 

이제, 당신은 (당신이 당신의 방법으로 인스턴스 변수에 액세스 할 필요가 없습니다 제공) 이런 식으로 뭔가를 시도 할 수 :

// In your header file 
+ (UIView *)showOptions:(NSInteger)tabNumber; 

// In your implementation file 
+ (UIView *)showOptions:(NSInteger)tabNumber{ 
    NSLog(@"show options called"); 

    UIView *optionsView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    optionsView.opaque = NO; 
    optionsView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f]; 
    //creating several buttons on optionsView 
    optionsView.tag = 100; 

    return optionsView; 
} 
그리고 마지막으로 이런 메시지를 보내

UIView *optionsView = [optionsMenu showOptions:4]; //Sending message to Class here 

마지막을 표시하기 위해 하위 뷰로보기를 추가 할 물론 잊지 마세요합니다. 이것이 의미가되기를 바랍니다 ...

+0

이제 완벽하게 작동합니다. 고마워요! –

관련 문제