2014-09-04 3 views
0

.H 파일- 아이폰 OS

#import <Foundation/Foundation.h> 

@interface globalFunction : NSObject{ 

    int nbr; 
    } 
@property (nonatomic) NSInteger myInt; 

+(void)eventCount:(NSString*) eventName; 

@end 

하는 .m 파일

@synthesize myInt; 


+(void)eventCount:(NSString *)eventName{ 


    myInt ++; 
    NSLog(@"Event name %@ and the count %d",eventName, myInt); 


    } 

을하지만,이 날에 액세스 인스턴스 변수 민트의 오류를 제공 클래스 메서드. (-) 내가 구글 검색으로

, 그것은 문제가 내가 인스턴스 기능에 내 글로벌 방법 기호 (+)를 변경할 때 고정 밝혀졌다

하지만 난의 ViewController의에서 사용할 수있는 전 세계적인 방법을 필요로하는 세션 중에 특정 일이 몇 번이나 일어 났는지 계산할 수있게 도와주세요.

이 문제를 어떻게 해결할 수 있습니까?

답변

0

사용 singelton 클래스에 글로벌 카운터 :

Interface: (in .h) 

@interface globalFunction : NSObject 
{ 
    int nbr; 
} 


@property (nonatomic, assign) NSInteger myInt; 

+ (instancetype)sharedInstance; 

- (void)eventCount:(NSString*) eventName; 

@end 




Implementation: (in .m) 

@implementation globalFunction 


#pragma mark - Singleton Class instance 
/* 
* Singelton instance of globalFunction 
*/ 
+ (instancetype)sharedInstance { 
    static globalFunction *_instance = nil; 
    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken, ^{ 
     _instance = [[globalFunction alloc] init]; 
    }); 

    return _instance; 
} 



- (void)eventCount:(NSString *)eventName{ 


    _myInt ++; 
    NSLog(@"Event name %@ and the count %d",eventName, _myInt); 
} 


@end 

및 사용에 액세스 할 수 있습니다 :

같은

[[globalFunction sharedInstance] eventCount:@"Ev"]; 
0

당신은 모든 객체간에 살아 있고 공유 될 싱글 톤 객체를 사용해야합니다. 그런 다음 재산으로 카운터를 유지하고이 게시물을 살펴보십시오. http://www.galloway.me.uk/tutorials/singleton-classes/

도움이 되었으면합니다.