NSThread presentviewcontroller调用?
问题描述:
我如何在一个线程中启动另一个viewcontroller?NSThread presentviewcontroller调用?
我的代码不起作用:
- (IBAction)btnGotoNextVC:(id)sender
{
[self.isLoading startAnimating];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[NSThread detachNewThreadSelector:@selector(gotoSecondController:)
toTarget:self
withObject:[NSArray arrayWithObjects:@"hello there", nil]];
}
和我的线程:
- (void) gotoSecondController: (NSArray*) parameters
{
NSString* data1 = parameters[0];
NSLog(@"%@", parameters[0]);
ViewController2 *VC2 = [self.storyboard instantiateViewControllerWithIdentifier:@"myView2"];
VC2.global_myLabel = [NSString stringWithFormat:@"Hallo %@", data1];
[self presentViewController:VC2 animated:YES completion:nil];
}
它是由该行崩溃:
[self presentViewController:VC2 animated:YES completion:nil];
错误是:
-[NSStringDrawingContext animationDidStart:]: unrecognized selector sent to instance 0x8f983c0
我能做些什么?感谢您的回答!
答
没有,任何更新UI,必须在主线程上运行。
要解决你的代码,你将不得不在主线程上运行英寸最简单的方法是直接调用该方法,becasue IBActions总是在主线程中调用:
[self gotoSecondController:@[@"hello there"]];
但是,如果你不是在主线程已经,你可以做一些代码运行在主线程以几种不同的方式。随着块:
__block MyViewController *blockSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
[blockSelf gotoSecondController:@[@"hello there"]];
});
,或者使用方法
[self performSelectorOnMainThread:@selector(gotoSecondController:) withObject:@[@"hello there"] waitUntilDone:NO];
谢谢您的回答! 但我有一个的UITableView和我从网页获得源代码,并解析它。 现在,当我调用从页面读取源代码的函数时,UI会出现问题。我怎样才能解决这个问题? 第一控制器stucks当我点击按钮和阅读网站代码.. 然而,感谢您的快速答复。 – user3032152
我想这取决于是什么让它吸...你是加载或解析主线程上的所有HTML?这会把事情搞定。你可以在一个单独的线程上创建并传递数据给一个UIViewController,然后将它显示在主线程中,这对你有用吗?请注意,我不确定您是否可以在单独的线程上更改标签文本,因此您必须在后台线程中处理该标签文本,然后更新主线程上的标签 – cjwirth