2014-06-30 5 views
-1

한 번에 여러 개체에 메시지를 보내고 각각에서 값을 전달해야합니다. 대리인은 한 번에 하나씩 만 사용할 수 있으며 알림은 값을 반환 할 수 없습니다.여러보기 컨트롤러에 알리고 알리미에 값을 다시 전달

두 개의보기 컨트롤러가 포함 된 탭 막대 컨트롤러가 반환 값을 사용하여 동시에 둘 모두에 알릴 수있는 방법이 있습니까?

당신은 키 - 값 쌍을 생성하고 통지의 사용자 정보 사전에 그 전달할 수
+0

알림을 사용하여 가치를 전달할 수 있습니다 ... –

+0

@SunnyShah 및 알림을 사용하여 값을 반환하는 방법은 무엇입니까? –

+0

@ S.J 왜 필요한 값을 반환 할뿐만 아니라 알림을 보내는 어떤 방법으로이를 감쌀 수없는 이유는 무엇입니까? 문제를 이해한다면 ... – fgeorgiew

답변

0

: 당신은 사용자 정보 사전을 전달할 수 있습니다

- (void)handleMyNotification:(NSNotification *)notification 
{ 
    NSDictionary *myDictionary = notification.userInfo; 
} 
+0

및 값 반환 방법 알림 사용 하시겠습니까? –

+0

값은 사전에 있습니다. 아래 표를 가져 주셔서 감사합니다. – Steve

0

: 당신은 통지를 처리 ​​할 경우 방법에

[[NSNotificationCenter defaultCenter] postNotificationName:myNotification object:nil userInfo:myDictionary]; 

알림에 :

NSDictionary *dataDict = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:isReachable] 
               forKey:@"isReachable"]; 

[[NSNotificationCenter defaultCenter] postNotificationName:@"reachabilityChanged" object:self userInfo:dataDict]; 

나는 그 문제가 다른 것으로 생각합니다. 전에. 그냥 알림을 보냅니다 객체의 내부 직후 일부 값을 통지를 보내고 반환하려면 아마

확인 다른 컨트롤러에 값을 전달하기 위해 당신은 NSNotification을 사용할 수 있습니다

- (NSString *)notifyAboutSomethingFancy 
{ 
    //the code from above 
    return @"whatever you want to return"; 
} 
0

몇 가지 방법을 포장 것 관련 링크

pass data in the userDictionary

+0

투표가 끝나기 전에 대답이 잘못되었는지 확인해야합니다. 당신의 과소 평가에 따라 투표하지 마십시오. –

+2

문제를 해결하든하지 않든 투표는 정당하지 않다. @ Sunny와 확실히 동의한다. –

1

나는 질문 통지를 보낸 사람에게 다시 응답을 전송하는 통지의 수신자를하는 방법입니다 가정합니다.

알림에서 값을 반환 할 수 없습니다. 그러나 알림 처리기는 원본 객체에서 메서드를 다시 호출하여 다시 전달할 데이터를 제공 할 수 있습니다.

알림을 전송 한 사람을 지정하는 postNotificationNameobject 매개 변수를 제공하는 한 가지 방법이 있습니다. 그런 다음 object (notificationSender)이 일부 프로토콜을 준수하는지 확인한 다음 일부 API를 사용합니다. 예를 들어, 뷰 컨트롤러가 호출 방법을위한 프로토콜을 정의 :

@protocol MyNotificationProtocol <NSObject> 

- (void)didReceiveNotificationResponse:(NSString *)response; 

@end 

을 그런를, 통지를 발행하는 목적은이 프로토콜을 준수합니다 :

@interface MyObject : NSObject <MyNotificationProtocol> 
@end 

@implementation MyObject 

- (void)notify 
{ 
    NSDictionary *userInfo = ...; 

    // note, supply a `object` that is the `notificationSender` 

    [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationName object:self userInfo:userInfo]; 
} 

- (void)didReceiveNotificationResponse:(NSString *)response 
{ 
    NSLog(@"response = %@", response); 
} 

@end 

그런 다음, 뷰 컨트롤러

을 받도록 셀만

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveNotification:) name:kNotificationName object:nil]; 
} 

- (void)dealloc 
{ 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
} 

- (void)didReceiveNotification:(NSNotification *)notification 
{ 
    id<MyNotificationProtocol> sender = notification.object; 

    // create your response to the notification however you want 

    NSString *response = ... 

    // now send the response 

    if ([sender conformsToProtocol:@protocol(MyNotificationProtocol)]) { 
     [sender didReceiveNotificationResponse:response]; 
    } 
} 

위의 예에서 : 상기 통지 응답을 보낼 NSNotificationobject 오브젝트의 파라미터를 사용할 수있다 응답은 NSString이지만 원하는 유형의 매개 변수를 didReceiveNotificationResponse에 사용하거나 매개 변수를 추가 할 수 있습니다 (예 : 응답의 sender).

관련 문제