투케이2K

333. (ios/objc) 오브젝티브 씨 뷰 컨트롤러 (ViewController) 라이프 사이클 정의 실시 - NSNotificationCenter 본문

IOS

333. (ios/objc) 오브젝티브 씨 뷰 컨트롤러 (ViewController) 라이프 사이클 정의 실시 - NSNotificationCenter

투케이2K 2022. 12. 9. 16:47
반응형

[개발 환경 설정]

개발 툴 : XCODE

개발 언어 : Objc

 

[소스 코드]

#import "ViewController.h"

// MARK: - [프로젝트-Swift.h import 명시]
#import "objectTest-Swift.h"



// MARK: - [Private 변수 선언 영역]
@interface ViewController (){

}
@end



// MARK: - [몸체 (구현부) 동작 작성]
@implementation ViewController



// MARK: - [클래스 설명]
/*
// -----------------------------------------
1. ViewController (구현부)
2. ios 13 이상 사용 : API_AVAILABLE(ios(13.0))
// -----------------------------------------
*/



// MARK: - [뷰 로드 실시]
- (void)viewDidLoad {
    [super viewDidLoad];
    printf("\n");
    printf("=============================== \n");
    printf("[ViewController >> viewDidLoad() :: 뷰 로드 실시] \n");
    printf("=============================== \n");
    printf("\n");
    
    
    // [포그라운드 및 백그라운드 감지를 위한 노티피케이션 등록 실시]
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(checkForeground:) // [상태 감지 메소드]
     name:UIApplicationWillEnterForegroundNotification // [포그라운드]
     object:nil
    ];
    
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(checkBackground:) // [상태 감지 메소드]
     name:UIApplicationDidEnterBackgroundNotification // [백그라운드]
     object:nil
    ];
}



// MARK: - [뷰 로드 완료]
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    printf("\n");
    printf("=============================== \n");
    printf("[ViewController >> viewWillAppear() :: 뷰 로드 완료] \n");
    printf("=============================== \n");
    printf("\n");
}



// MARK: - [뷰 화면 표시]
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    printf("\n");
    printf("=============================== \n");
    printf("[ViewController >> viewDidAppear() :: 뷰 화면 표시] \n");
    printf("=============================== \n");
    printf("\n");
}



// MARK: - [뷰 정지 상태]
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    printf("\n");
    printf("=============================== \n");
    printf("[ViewController >> viewWillDisappear() :: 뷰 정지 상태] \n");
    printf("=============================== \n");
    printf("\n");
}



// MARK: - [뷰 종료 상태]
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    printf("\n");
    printf("=============================== \n");
    printf("[ViewController >> viewDidDisappear() :: 뷰 종료 상태] \n");
    printf("=============================== \n");
    printf("\n");
    
    
    // [포그라운드 및 백그라운드 상태 감지 해제 실시]
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}



// MARK: - [포그라운드 상태 로직 처리 메소드]
- (void)checkForeground:(NSNotification *) note {
    printf("\n");
    printf("=============================== \n");
    printf("[ViewController >> checkForeground() :: 뷰 컨트롤러 포그라운드 상태] \n");
    printf("=============================== \n");
    printf("\n");
}



// MARK: - [백그라운드 상태 로직 처리 메소드]
- (void)checkBackground:(NSNotification *) note {
    printf("\n");
    printf("=============================== \n");
    printf("[ViewController >> checkBackground() :: 뷰 컨트롤러 백그라운드 상태] \n");
    printf("=============================== \n");
    printf("\n");
}



// -----------------------------------------
@end
// -----------------------------------------
 

[결과 출력]


반응형
Comments