0

我有一个UIViewControllerwith 按钮,它带来了另一个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");
}

我认为我的问题是:它不在 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
4

1 回答 1

0

I don't know why you want to use a notification here, when you can perform the action directly without issue. But a simple thing you can do in notification methods that need to update UI is to just have them call themselves on the main thread if they're not already running on that thread:

-(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];
}
于 2013-03-24T04:52:32.957 回答