programing

NSNotification 센터의 개체 속성을 사용하는 방법

telebox 2023. 8. 20. 10:45
반응형

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

반응형