-- iOS (iPhone)
global variable 만들기
어린왕자악꿍
2015. 1. 18. 21:25
글로벌로 변수를 만들어 사용하기 위해서 AppDelegate에 아래와 같이 변수를 선언한다.
[AppDelegate.h]
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
NSString* mGlobalStr;
BOOL mIsGlobalFlag;
}
@property (nonatomic, retain) NSString * mGlobalStr;
@property (nonatomic, assign) BOOL mIsGlobalFlag;
[AppDelegate.m]
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize mGlobalStr;
@synthesize mIsGlobalFlag;
그리고, 다른 class에서 아래와 같이 AppDelegate를 인스턴스하여 변수를 이용한다.
AppDelegate *mApp = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if(mApp. mIsGlobalFlag)
NSLog(@"%@", mApp. mGlobalStr);