0

UIViewController 단추가있는 UIViewController 단추가 있습니다. 버튼을 클릭하면 내 NSLog과 같이 표시되며 완료되면 다른 viewcontroller을로드하라는 알림을 보내려고합니다. 글쎄, 모든 일이 올바르게 끝났다고해도, 어떻게 든 작동하지 않고 UIViewController이 나타나지 않습니다. 여기 코드는 다음과 같습니다NSNotificationCenter 문제 및 UIViewControlletr로드

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



-(void) infoPage:(NSNotification*)notification 
{ 
    NSLog(@"Code executing in Thread %@",[NSThread currentThread]); 

    InfoCtrol *i = [[InfoCtrol alloc] init]; 
    i.hidesBottomBarWhenPushed = YES; 
    [self.navigationController pushViewController:i animated:YES]; 
} 

내 tabbaritem 버튼

-(void)info { 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"InfoPage" 
                 object:nil 
                 userInfo:nil]; 
    NSLog(@"test not"); 
} 

내가 생각하는 내 문제가 있다는 것입니다 : 그것은 mainThread에 아니지만 난 그 해결 방법을 모르겠어요 :

나는이를 사용하지만있는 UIViewController를 가져 오지 않았다

[self performSelectorOnMainThread:@selector(test) withObject:nil waitUntilDone:NO]; 

-(void)test{ 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"InfoPage" 
                 object:nil 
                 userInfo:nil]; 
} 

난 그냥 버튼이 코드를 삽입하면, UIViewController를 표시하지만 내가 사용하려는 NSNotificationCenter

InfoCtrol *i = [[InfoCtrol alloc] init]; 
    i.hidesBottomBarWhenPushed = YES; 
[self.navigationController pushViewController:i animated:YES]; 

내 로그인 :

Code executing in Thread <NSThread: 0x1fd7c7e0>{name = (null), num = 1} 

업데이트 : 당신은 통지를 사용하려는 이유

How should i remove last thread from mainThread 
+0

모든 UIKit 작업은 주 스레드에서 발생합니다. 스레드를 사용하여 다른 스레드로 고의로 이동해야합니다. 그랜드 센트럴 파견 (Central Central Dispatch), 다른 스레드에 있다는 것은 문제가되지 않을 것입니다. 하지만,'NSThread'에'isMainThread'라고 불리는 클래스 메소드가 있습니다. – MaxGabriel

+0

이 코드 스 니펫은 어떤 컨트롤러에 있습니까? 가지고있는 컨트롤러와이 코드가있는 곳을 설명해야합니다. – rdelmar

+0

'self.navigationController'가'nil'이 아님을 확인하십시오 –

답변

0

나도 몰라 여기서 문제없이 직접 작업을 수행 할 수 있습니다. 그러나 UI를 업데이트해야하는 알림 메서드에서 수행 할 수있는 간단한 작업은 해당 스레드에서 아직 실행 중이 지 않은 경우 주 스레드에서 호출하도록하는 것입니다.

-(void)myNotificationMethod:(NSNotification*)note { 
    if (![NSThread isMainThread]) { 
     [self performSelectorOnMainThread:@selector(myNotificationMethod:) 
           withObject:note 
          waitUntilDone:NO]; 
     return; 
    } 

    // ... do some UI stuff 
    InfoCtrol *i = [[InfoCtrol alloc] init]; 
    [self.navigationController pushViewController:i animated:YES]; 
} 
+0

@SeamusCambell 여전히 문제는 uiviewcontroller를로드하지 않습니다. NSNotification 센터를 사용하는 이유입니다. 많은 정보 버튼이 있습니다. 알고 계십니까? 왜 이름에 null이 있습니까? : {name = (null), num = 1} –

+0

'name'은 스레드에서 설정할 수있는 속성입니다. 꼭 필요한 것은 아니며 단지 편의를위한 것이라고 생각합니다. 그것은 어떤 이유로 든'self.navigationController'가'nil'이라고 여기는 것 같습니다. –

+0

나는 정상적인 방법으로 클래스를 호출 할 때 한 가지를 말하는 것을 잊어 버렸습니다. 내보기에서 tabbar 항목을 숨기면 내보기 위에있는 tabbar보기를 의미하지만 NSSnotificationCenter에서는보기가 올 수 없습니다. –