问题描述:
有如下3个UIViewController
StartViewController:除了一个View外什么也没有
LoadViewController:一个View和一个Socket
GameViewController:一个View,View里的button,View里的ImageView,一个Socket
逻辑流程如下:
第一步:在StartViewController.m中先加载LoadView
loadViewController = [[LoadViewController alloc] initWithNibName:[AppDelegate currentDelegate].loadNibFileName bundle:nil];
[self.view addSubview:loadViewController.view];
第二步:LoadView是关于Socket通讯,在处理Socket通讯的时候用到如下函数:(没有启动额外的线程,依然是主线程)-(void)statusProcess:(uint8_t)statusCode {
switch (statusCode) {
........
case SOCKET_RCVING: {
S32 result = ........;
switch (result) {
case 1: {
//发送消息,初始化GameViewController
[[AppDelegate currentDelegate].startViewController performSelector:@selector(initGameViewController)];
break;
}
case 2: {
....
}
}
break;
}
......
default:
break;
}
}
第三步:初始化GameViewController
-(void)initGameViewController {
gameViewController = [[GameViewController alloc] initWithNibName:[AppDelegate currentDelegate].gameNibFileName bundle:nil];
[gameViewController createServer];
}
第四步:创建gameViewController的Socket和执行Socket,依然是statusProcess函数-(void)statusProcess:(uint8_t)statusCode {
switch(statusCode) {
.....
case SOCKET_SENDING: {
//启动解析线程
[NSThread detachNewThreadSelector:@selector(dataAnalysisThread) toTarget:self withObject:nil];
break;
}
.....
default:
break;
}
}
第五步:启动解析线程dataAnalysisThread-(void)dataAnalysisThread {
float coreAnalysisTimer = 0;
do {
int flag = ...........;
switch (flag) {
.....
case xxx: {
[appDelegate.startViewController performSelectorOnMainThread:@selector(changeViewFromLoadViewToGameView) withObject:nil waitUntilDone:YES];
break;
}
case xxx2: {
//往ImageView里添加内容
break;
}
.....
default: {
//NSLog(@"获得其他tag :%d",flag);
break;
}
}
}while(xxxxxx);
}
第六步:显示GameViewController-(void)changeViewFromLoadViewToGameView {
[self removeLoadView];
[self.view addSubview:gameViewController.view];
}
此时就会出现GameViewController的button居然显示不了,ImageView可以正常显示
解决的办法是如下:
把第三步的函数更改成如下:
-(void)initGameViewController {
gameViewController = [[GameViewController alloc] initWithNibName:[AppDelegate currentDelegate].gameNibFileName bundle:nil];
[gameViewController createServer];
//这里非常的重要,解决button不显示的问题
[self.view insertSubview:gameViewController.view atIndex:1];
}
把第六步的函数更改成如下:
-(void)changeViewFromLoadViewToGameView {
//这里非常的重要,解决button不显示的问题
[self removeLoadView];
//[self.view addSubview:gameViewController.view];
}
这样就可以正常的显示button和ImageView了
在initGameViewController的时候,就把View加入到self.view中,但是被loadView给遮住了,
在changeViewFromLoadViewToGameView的时候,把loadView给remove掉
花了6,7个多小时才解决这个问题。