【发布时间】:2014-03-11 17:01:39
【问题描述】:
我有一个称为滚动器的 UIScrollView。它具有自定义 UIButtons 的子视图。我尝试在添加之前和之后打印子视图的数量,如下所示
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 target:self selector:@selector(checkForTiles) userInfo:nil repeats:YES];的按钮
更新:: 添加子视图的代码
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];}];
}
}
【问题讨论】:
-
你能发布代码来为你的滚动条添加视图吗?
标签: ios uiscrollview subviews