Ios UIScrollView:已添加子视图,但未显示
我有一个叫做scroller的UIScrollView。它有子视图,这些子视图是自定义的UIButton..我尝试在添加之前和之后打印子视图的数量,如下所示Ios UIScrollView:已添加子视图,但未显示,ios,uiscrollview,subviews,Ios,Uiscrollview,Subviews,我有一个叫做scroller的UIScrollView。它有子视图,这些子视图是自定义的UIButton..我尝试在添加之前和之后打印子视图的数量,如下所示 NSLog(@"Adding %d b4 %d",buttonno, [[self.scroller subviews] count]); [self.scroller addSubview:menuBtn]; NSLog(@"Adding %d after %d",butto
NSLog(@"Adding %d b4 %d",buttonno, [[self.scroller subviews] count]);
[self.scroller addSubview:menuBtn];
NSLog(@"Adding %d after %d",buttonno, self.scroller.subviews.count);
子视图未显示。但是子视图的数量会增加,例如,它的输出如下
2014-03-11 17:53:49.863 PC2[4519:60b]添加1 b4 10
2014-03-11 17:53:49.872 PC2[4519:60b]在11之后添加1
但是当我尝试使用运行代码的测试按钮打印子视图的数量时
NSLog(@"Buttons subviews: %d",self.scroller.subviews.count);
我可以看到没有添加子视图。当我在调试器中看到变量时,我也可以看到没有添加子视图
顺便说一句,我正在添加基于计时器[NSTimer scheduledTimerWithTimeInterval:0.2目标:自选择器:@selector(checkForTiles)userInfo:nil repeats:YES]的按钮代码>
Update::添加子视图的代码
if (!alreadyExists)
{
NSLog(@"Adding %d b4 %d",buttonno, [[self.scroller subviews] count]);
//AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
pccButtonViewController *menuBtn=nil;
menuBtn=[pccButtonViewController alloc ];
menuBtn = [pccButtonViewController buttonWithType:UIButtonTypeCustom];
menuBtn.no=buttonno;
menuBtn.slotNo=buttonCount;
menuBtn.frame = [[slotFrames objectAtIndex:menuBtn.slotNo] CGRectValue];
[menuBtn setBackgroundImage:[UIImage imageNamed:[[NSString alloc] initWithFormat:@"L%02d_Tile.jpg",buttonno]] forState:UIControlStateNormal];
[menuBtn addTarget:self action:@selector(miniButtons:) forControlEvents:UIControlEventTouchUpInside];
menuBtn.no=buttonno;
menuBtn.alpha=0.0f;
[self.scroller addSubview:menuBtn];
NSLog(@"Adding %d after %d",buttonno, self.scroller.subviews.count);
//NSLog(@"Subview Added %d btn no:%@",buttonno,[slotFrames objectAtIndex:menuBtn.slotNo]);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0f];
//self.menuBtn.frame = CGRectMake(8, 10, 50, 60);
menuBtn.alpha=1.0f;
[UIView commitAnimations];
[buttons addObject:menuBtn];
buttonCount++;
}
buttonclicked=0;
[self.scroller setContentSize:CGSizeMake([self.scroller bounds].size.width,(ceil(buttonCount/tilesPerRow)+1)*[self.scroller bounds].size.width/tilesPerRow/2*3)];
appdelegate.Sharevar.vidsyncButtons=self.scroller.subviews.copy;//Used to maintain State when the view is presented for the second time
当视图第一次运行时,此操作可以正常工作。当视图第二次运行时会出现问题。.我从appdelegate中删除视图,如下所示
-(void)removeAnyView
{
NSLog(@"Removing");
if (viewonScreen)
{
viewonScreen=false;
[self.inPort prepareToBeDeleted];
self.inPort=Nil;
[self.window.rootViewController dismissViewControllerAnimated:NO completion:^{[self presentView];}];
}
}
对UI的任何更新都必须在主线程上,而您的NSTimer不会在主线程上,因此您需要执行以下操作
dispatch_sync(dispatch_get_main_queue(), ^{
[self.scroller addSubview:menuBtn];
});
改变这个
menuBtn=[pccButtonViewController alloc ];
对此
menuBtn=[[pccButtonViewController alloc ] init];
来自文档:
按钮类型:
此方法是创建按钮对象的方便构造函数
具有特定的配置。如果将UIButton子类化,则此方法
不返回子类的实例。如果要创建
如果是特定子类的实例,则必须alloc/init该按钮
直接的
已经很长时间了。。我忘了这个问题。。该错误是由于未使NSTimer无效而导致的。。必须小心这些..您能发布代码来添加视图到您的滚动条吗?我在函数checkforTiles“[self-performSelectorOnMainThread:@selector(addTile:)withObject:content waitUntilDone:YES];”这还不够吗?我想你必须发布更多的代码,这样人们才能看到发生了什么。目前没有足够的资源像你建议的那样进行修改,应用程序挂起。没有响应。它在执行时卡住了。现在抛出了错误。问题仍然存在