ios NSNotificationCenter 알림 의 간단 한 사용
3014 단어 iosNSNotificationCenter통지 하 다.
//
[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 코드 를 붙 이지 않 습 니 다.이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Swift의 패스트 패스Objective-C를 대체하기 위해 만들어졌지만 Xcode는 Objective-C 런타임 라이브러리를 사용하기 때문에 Swift와 함께 C, C++ 및 Objective-C를 컴파일할 수 있습니다. Xcode는 S...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.