블로그 이미지
Sunny's

calendar

1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

Notice

'NSNotification'에 해당되는 글 2

  1. 2012.03.28 otification 사용하기 (다른 객체에게 메시지 전달)1
  2. 2012.03.02 NSNotificationcenter 사용
2012. 3. 28. 21:03 IPhone

 

 

전달 받을 컨트롤러 에서 NotificationCenter에 옵져버 등록 한다.

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(myFunction:) name:@"msgName" object:nil];


myFunction 함수 구현도 필요
-(void) myFunction:(NSNotification *) note {
[[note userInfo] objectForKey:@"myKey"]; // 메시지의 내의 myKey 라는 값을 가져온다.
}


통지를 보내는 쪽 에서 처리
// myKey라는 키로 value 라는 문자열을 전달 한다.
NSDictionary *dic = [NSDictionary dictionaryWithOjbect:@"value" forKey:@"myKey"];

NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName:@"msgName" object:self userInfo:dic];


통지를 보내면 통지를 등록한 컨트롤로에 정의된 myFunction 함수가 호출된다.

◆ 데이터 여러개 보낼때
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:EDIT_MAIN_TABLE], @"type",[NSNumber numberWithBool:YES], @"edit", nil];

◆ 데이터 하나만 보낼때
NSDictionary *dic = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:CARD_TYPE_MEMBERSHIP] forKey:@"type"];

출처 : http://comxp.tistory.com/188 


posted by Sunny's
2012. 3. 2. 17:29 IPhone

Use NSNotification

출처 : http://stackoverflow.com/questions/2191594/how-to-send-and-receive-message-through-nsnotificationcenter-in-objective-c 


 @implementation TestClass

- (void) dealloc
{
   
// If you don't remove yourself as an observer, the Notification Center
   
// will continue to try and send notification objects to the deallocated
   
// object.
   
[[NSNotificationCenter defaultCenter] removeObserver:self];
   
[super dealloc];
}

- (id) init
{
    self
= [super init];
   
if (!self) return nil;

   
// Add this instance of TestClass as an observer of the TestNotification.
   
// We tell the notification center to inform us of "TestNotification"
   
// notifications using the receiveTestNotification: selector. By
   
// specifying object:nil, we tell the notification center that we are not
   
// interested in who posted the notification. If you provided an actual
   
// object rather than nil, the notification center will only notify you
   
// when the notification was posted by that particular object.

   
[[NSNotificationCenter defaultCenter] addObserver:self
        selector
:@selector(receiveTestNotification:)
        name
:@"TestNotification"
        object
:nil];

   
return self;
}

- (void) receiveTestNotification:(NSNotification *) notification
{
   
// [notification name] should always be @"TestNotification"
   
// unless you use this method for observation of other notifications
   
// as well.

   
if ([[notification name] isEqualToString:@"TestNotification"])
       
NSLog (@"Successfully received the test notification!");
}

@end

... somewhere else in another class ...

- (void) someMethod
{

   
// All instances of TestClass will be notified
   
[[NSNotificationCenter defaultCenter]
        postNotificationName
:@"TestNotification"
        object
:self];

}
posted by Sunny's
prev 1 next