2013-10-17 2 views
1

내 앱에 UITableview 컨트롤러, 뷰 컨트롤러가 있고 NSNotificationCenter를 사용하여 UITableview 컨트롤러의 NSDictionary를 내 ViewController로 전달하려고합니다.NSNotificationCenter, 메모리 주소를 인쇄합니다.

의 ViewController : 같은 메모리 결과를 그래서, 난 내 jQuery과 컨트롤러에 알림을 밀어 그때라고 내 ViewController.The 선택기에서 선택기를 사용하여 관찰자를 추가 할 수 있지만 나는 NSLog가 얻을 0x8a0bcc0을

NSDictionary 대신 NSString을 전달하려고했지만 문자열 결과가 아닌 메모리 결과가 다시 나타납니다.

내 코드

:

:

jQuery과 컨트롤러

NSString *[email protected]"This is a test string"; 
    [[NSNotificationCenter defaultCenter] postNotificationName: @"Update" object: string]; 

의 ViewController 여기

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(incomingNotification:) name:@"Update" object:nil]; 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Update" object:self]; 

그리고는 incomingNotification 선택 방법입니다

모든 알림은 ViewDidLoad 메서드에서 발생합니다. 감사합니다!

UPDATE

마지막으로, 나는 내 TableViewController에서 조금에게 inheretence을 변경, 데이터를 전달하는 NSNotificationCenter 및 사용 속성을 사용하여 종료합니다. Notifications가 작동하지 않는 이유는 알 수 없습니다. 귀하의 제안과 아이디어에 대해 모두 고맙습니다 :)

+1

왜 옵저버를 추가 한 직후에 두 번째 알림을'ViewController'에 게시합니까? 그게 콘솔에서이 문자열을 얻은 이유입니다. 테스트 문자열이없는 이유는 무엇입니까? - 테이블 컨트롤러에 알림을 게시하기 전에 옵저버를 추가했는지 확인하십시오 (해당 순서를 보도록 중단 점을 설정하십시오). – medvedNick

+0

ViewController에서 postNotification을 호출하여 자신이 직접 인쇄합니다. 목적. –

+0

ViewController에서 두 번째 알림을 삭제하면 선택기 메서드가 호출되지 않습니다! 이제 중단 점을 설정하려고합니다! –

답변

1
[[NSNotificationCenter defaultCenter] postNotificationName:@"Update" object:self] 

개체는 알림을 생성하는 개체를 의미합니다. 만약 내가 제대로 이해하고 매개 변수를 다른 방법

[[NSNotificationCenter defaultCenter] postNotificationName:@"Update" object:self userInfo:string] 
+0

당신의 제안을 해주셔서 감사합니다,하지만 다시 선택기 메서드는 불필요한 ViewController observer.When 내가 지금 - 당신의 제안을 사용하여, 내가 다시 메모리 주소 결과를 얻을 삭제되지 않습니다. –

+0

'userInfo'는 사전이되어야합니다. 그러나 이것은 사소한 변화입니다. – Tommy

+0

네, 그게 문제라고 생각하지 않습니다 :) –

0

을 사용하여 게시하려면 UIViewControllerUITableViewController의 버튼을 누르 후 표시됩니다. 그리고 을 -viewDidLoad:에 옵저버로 추가하면로드 될 때만 알림을받을 수 있습니다.

당신은 무엇을해야합니까 :

-(id) init 
{ 
    self = [super init]; 
    if (self) 
    { 
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(incomingNotification:) name:@"Update" object:nil]; 
    } 
    return self; 
} 

그래서 당신이 ViewController 처음부터 알림 관찰되어 있는지 확인 할 수있다 (물론,이 힘 :

1) -init 또는 같은 ViewController-initWithNibName: 메소드를 오버라이드 (override) 귀하의 경우에 불필요한 단계 일 수 있습니다.) ViewController을 누를 때 다음과 같이 알림을 보내야합니다.

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    ViewController *nextController = [[ViewController alloc] initWithNibName:nil bundle:nil]; 
    [self.navigationController pushViewController:nextController animated:YES]; 

    NSString *[email protected]"This is a test string"; 
    [[NSNotificationCenter defaultCenter] postNotificationName: @"Update" object: string]; 
} 

그러나 하나의보기 컨트롤러에서 다른 매개 변수로 매개 변수를 보내려한다면이 방법이 좋습니다.ViewController에 속성을 만들고 -tableView:didSelectRowAtIndex:UITableViewController으로 설정하면이 속성이

+0

대단히 고맙다고 생각합니다! 당신은 그 사건을 정확히 이해했습니다. 당신이 말한 것을 시도했지만 아무 일도 없었습니다. 내 ViewController는 스토리 보드에서 만들어졌으며, 저는 사용합니다. performSegueWithIdentifier 내 - (void)에서 segue 수행 할 tableView : (UITableView *) tableView didSelectRowAtIndexPath : (NSIndexPath *) indexPath 대리자 메서드입니다. 그래서, 내가 ViewController.Moreover 할당해서는 안 이유, NSNotificationCenter를 사용하는 이유는 사실 내가 UITableviewController 수 ViewController 하위 클래스 및 정보를 얻을 수 있습니다! –

+0

닉, 나는 또한 initwithCoder 메서드를 시도했다. 이것은 또한 작동하지 않습니다. –

+0

'ViewController'의'-prepareForSegue : sender :'메소드에 알림을 신청하고'-performSegueWithIdentifier'의 바로 뒤에 알림을 게시하십시오. 또한 segue에서'ViewController'를 얻을 수 있습니다 :'[segue destinationViewController];'그래서 당신은 알림을 사용하는 대신에 속성을 설정할 수 있습니다. – medvedNick

관련 문제