Ios NSNotificationCenter和加载UIViewControlletr时出现问题

Ios NSNotificationCenter和加载UIViewControlletr时出现问题,ios,objective-c,uiviewcontroller,nsthread,nsnotificationcenter,Ios,Objective C,Uiviewcontroller,Nsthread,Nsnotificationcenter,我有一个带有按钮的UIViewController,该按钮带来另一个UIViewController。单击按钮,如我的NSLog中所示,完成后,我想发送一个通知以加载另一个viewcontroller。好吧,虽然看起来一切都做对了,但不知怎么的,它不起作用,UIViewController没有出现。代码如下: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(infoPage:)

我有一个带有按钮的
UIViewController
,该按钮带来另一个
UIViewController
。单击按钮,如我的
NSLog
中所示,完成后,我想发送一个通知以加载另一个
viewcontroller
。好吧,虽然看起来一切都做对了,但不知怎么的,它不起作用,
UIViewController
没有出现。代码如下:

 [[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");
}
我想我的问题是:它不在主线程中,但我不知道该如何解决它:

我也使用了这个,但它没有带来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}

我不知道您为什么要在这里使用通知,因为您可以直接执行操作而不会出现问题。但在需要更新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];
}

所有UIKit的事情都发生在主线程上,您必须使用Grand Central Dispatch等工具有目的地移动到另一个线程,因此不太可能出现在另一个线程上的问题。在
NSThread
上有一个名为
isMainThread
的类方法,您可以使用它来检查。这些代码段在哪些控制器中?您应该描述您拥有的控制器以及所有这些代码所在的位置。确保
self.navigationController
不是
nil
@GuoLuchuan我应该如何测试that@MaxGabriel请拿一些给我看看好吗code@SeamusCambell仍然存在问题,它没有加载uiviewcontroller,使用NSNotification Center的原因:我有很多信息按钮,您知道为什么我的名称为null吗?:{name=(null),num=1}
name
只是一个可以在线程上设置的属性。这不是必需的,我想这只是一种方便。在我看来,出于某种原因,
self.navigationController
在这里很可能是
nil
。我忘了说一件事,当我以正常方式调用该类时,我的视图中有tabbar项,我将隐藏它们,这意味着tabbar视图在我的视图上方,但使用NSSnotificationCenter,视图无法出现
-(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];
}