ios NSNotificationCenter 알림 의 간단 한 사용

알림 클래스 자체 가 비교적 간단 하 다.대략 등록 알림 모니터,발송 알림,로그아웃 알림 모니터 세 가지 방법 으로 나 뉜 다.알림 센터(NSNotificationCenter)는 하나의 예 를 사용 합 니 다.전체 시스템 은 하나의 알림 센터 만 있 고 다음 코드 를 통 해 가 져 옵 니 다.

//      
[NSNotificationCenter defaultCenter];
등록 알림 모니터 방법:

//observer    
//aSelector            
//aName         
//object        ,   postNotification object  ,        
- (void)addObserver:(id)observer selector:(SEL)aSelector name:(nullable NSNotificationName)aName object:(nullable id)anObject;
알림 보 내 는 방법:

//        NSNotification  
- (void)postNotification:(NSNotification *)notification;

//aName        
//anObject        ,            
- (void)postNotificationName:(NSNotificationName)aName object:(nullable id)anObject;

//aUserInfo        ,       
//                ,    。
- (void)postNotificationName:(NSNotificationName)aName object:(nullable id)anObject userInfo:(nullable NSDictionary *)aUserInfo;

로그아웃 알림 모니터 방법:

//        
- (void)removeObserver:(id)observer;
//        ,aName        ,anObject          
- (void)removeObserver:(id)observer name:(nullable NSNotificationName)aName object:(nullable id)anObject;
// block          
- (id <NSObject>)addObserverForName:(nullable NSNotificationName)name object:(nullable id)obj queue:(nullable NSOperationQueue *)queue usingBlock:(void (^)(NSNotification *note))block API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
사용 현황:
NSNotificationCenter 클래스 는 일반적으로 한 대상 이 다른 대상 에 게 이 벤트 를 전달 하 는 데 사용 되 며,다른 대상 에서 특정한 방법 을 터치 하면 크로스 보기 의 상호작용 을 실현 할 수 있다.나 는 최근 한 달 동안 NSNotification Center 류 를 두 번 이나 사용 했다.
① 프로젝트 를 국제 화 할 때 언어 를 전환 할 때 알림 방식 으로 다른 화면 을 새로 고침 한다(메 인 라인 에서).
② SGpagingView 를 사용 할 때 pageContentView 의 내용 이 다 중 선택 상태 일 때 pageTitleView 가 전환 을 금지 하 는 기능 이 필요 합 니 다.SGpaging View 가 제공 하 는 방법 을 봤 는데 이것 이 없어 서 NSNotificationCenter 를 사 용 했 습 니 다.다 중 선택 상태 에 들 어 갈 때 알림 을 보 내 고 다 중 선택 상태 에서 물 러 날 때 알림 을 보 냅 니 다.

//       
  [NotifyUtil addNotify:NOTIFY_DISABLE_SWITCH Observer:self selector:@selector(disableSwitch) Object:nil];
  [NotifyUtil addNotify:NOTIFY_ALLOW_SWITCH Observer:self selector:@selector(allowSwitch) Object:nil];

//    

//  pageTitleView    
-(void)disableSwitch{
  self.pageTitleView.userInteractionEnabled = NO;
}
//  pageTitleView    
-(void)allowSwitch{
  self.pageTitleView.userInteractionEnabled = YES;
}

//       
- (void) dealloc{
  [NotifyUtil removeNotify:NOTIFY_DISABLE_SWITCH Observer:self];
  [NotifyUtil removeNotify:NOTIFY_ALLOW_SWITCH Observer:self];
}

주:NotifyUtil 로 NSNotificationCenter 류 를 간단하게 봉 인 했 습 니 다.매개 변수 가 기본적으로 일치 하면 NotifyUtil 코드 를 붙 이지 않 습 니 다.
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기