我有一个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}
更新
How should i remove last thread from mainThread
答案 0 :(得分: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];
}