NSNotification 센터의 개체 속성을 사용하는 방법
NS Notification Center에서 개체 속성을 사용하는 방법을 보여줄 수 있는 사람이 있습니다.이를 사용하여 정수 값을 선택기 메서드에 전달할 수 있습니다.
이렇게 UI 보기에서 알림 수신기를 설정했습니다.정수 값이 전달되기를 원하기 때문에 0을 무엇으로 대체해야 할지 모르겠습니다.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveEvent:) name:@"myevent" object:nil];
- (void)receiveEvent:(NSNotification *)notification {
// handle event
NSLog(@"got event %@", notification);
}
저는 이렇게 다른 수업에서 알림을 보냅니다.함수는 인덱스라는 변수를 전달합니다.이것이 바로 제가 어떻게든 알림과 함께 발사하고 싶은 가치입니다.
-(void) disptachFunction:(int) index
{
int pass= (int)index;
[[NSNotificationCenter defaultCenter] postNotificationName:@"myevent" object:pass];
//[[NSNotificationCenter defaultCenter] postNotificationName:<#(NSString *)aName#> object:<#(id)anObject#>
}
그object
매개 변수는 알림의 보낸 사람을 나타냅니다. 일반적으로self
.
추가 정보를 전달하려면 다음을 사용해야 합니다.NSNotificationCenter
방법postNotificationName:object:userInfo:
(사용자가 자유롭게 정의할 수 있는) 값 사전을 사용합니다.내용은 실제와 같아야 합니다.NSObject
정수와 같은 정수 유형이 아닌 인스턴스이므로 정수 값을 다음으로 묶어야 합니다.NSNumber
물건들.
NSDictionary* dict = [NSDictionary dictionaryWithObject:
[NSNumber numberWithInt:index]
forKey:@"index"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"myevent"
object:self
userInfo:dict];
그object
재산은 그것에 적합하지 않습니다.대신에 당신은 사용하기를 원합니다.userinfo
매개변수:
+ (id)notificationWithName:(NSString *)aName
object:(id)anObject
userInfo:(NSDictionary *)userInfo
userInfo
는 알림과 함께 정보를 전송하기 위한 NSDictionary입니다.
당신의.dispatchFunction
대신 방법은 다음과 같습니다.
- (void) disptachFunction:(int) index {
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:index] forKey:@"pass"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"myevent" object:nil userInfo:userInfo];
}
당신의.receiveEvent
방법은 다음과 같습니다.
- (void)receiveEvent:(NSNotification *)notification {
int pass = [[[notification userInfo] valueForKey:@"pass"] intValue];
}
언급URL : https://stackoverflow.com/questions/4312338/how-to-use-the-object-property-of-nsnotificationcenter
'programing' 카테고리의 다른 글
Spring MVC에서 캐시 헤더를 어떻게 설정합니까? (0) | 2023.08.20 |
---|---|
HomeBrew의 MariaDB: 네트워크 액세스 활성화 (0) | 2023.08.20 |
nohup:입력을 취소하고 출력을 'up.out'에 추가합니다. (0) | 2023.08.20 |
현재 프로젝트 작업을 위해 IntelliJ IDEA 14에서 주석 처리를 구성하는 방법은 무엇입니까? (0) | 2023.08.20 |
TD의 첫 번째와 마지막 TD를 연속으로 선택하는 방법은 무엇입니까? (0) | 2023.08.20 |