【问题标题】:UIScrollView: subview added but not showing upUIScrollView:添加了子视图但未显示
【发布时间】: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


【解决方案1】:

好久不见了..我忘了这个问题..错误是由于没有使NSTimer失效..这些要小心..

【讨论】:

    【解决方案2】:

    对 UI 的任何更新都必须在主线程上,而你的 NSTimer 不会,所以你需要做这样的事情......

    dispatch_sync(dispatch_get_main_queue(), ^{
        [self.scroller addSubview:menuBtn];
    });
    

    【讨论】:

    • 我在函数 checkforTiles “[self performSelectorOnMainThread:@selector(addTile:) withObject:content waitUntilDone:YES];”中这样做这还不够吗??
    • 我认为您将不得不发布更多代码,以便人们可以看到发生了什么。目前还不够继续
    • 像你建议的那样尝试过,应用程序挂起..没有响应..它一直在执行它..现在抛出错误。
    【解决方案3】:

    改变这个

    menuBtn=[pccButtonViewController alloc ];
    

    到这里

    menuBtn=[[pccButtonViewController alloc ] init];
    

    来自文档:

    buttonWithType:

    此方法是用于创建按钮对象的便捷构造函数 具有特定配置。如果你继承 UIButton,这个方法 不返回您的子类的实例。如果你想创建一个 特定子类的实例,您必须分配/初始化按钮 直接。

    【讨论】:

    • 问题依旧