It's been a long time.. I forgot about this question.. The error was caused by not invalidating the NSTimer.. Got to be careful with these..
UIScrollView: subview added but not showing up
-
13-06-2023 - |
Question
I have a UIScrollView called scroller. It has subviews which are custom UIButtons..I try to print the number of subviews before and after adding like as follows
NSLog(@"Adding %d b4 %d",buttonno, [[self.scroller subviews] count]);
[self.scroller addSubview:menuBtn];
NSLog(@"Adding %d after %d",buttonno, self.scroller.subviews.count);
The subview is not showing up. But the number of subviews increments for example the output of this is like
2014-03-11 17:53:49.863 PC2[4519:60b] Adding 1 b4 10
2014-03-11 17:53:49.872 PC2[4519:60b] Adding 1 after 11
But when I try to print the number of subviews using a test button which runs the code
NSLog(@"Buttons subviews: %d",self.scroller.subviews.count);
I can see that no subview is added..I can also see that no subview is added when I see the variables in the debugger.
btw I'm adding buttons based on timer [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(checkForTiles) userInfo:nil repeats:YES];
Update:: The code to add subview
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
This works fine when the View runs for the first time.The problems arise when it is run for the second time..I remove the view from appdelegate like this
-(void)removeAnyView
{
NSLog(@"Removing");
if (viewonScreen)
{
viewonScreen=false;
[self.inPort prepareToBeDeleted];
self.inPort=Nil;
[self.window.rootViewController dismissViewControllerAnimated:NO completion:^{[self presentView];}];
}
}
Solution
OTHER TIPS
Any updates to the UI have to be on the main thread, which your NSTimer won't be, so you need to do something like this...
dispatch_sync(dispatch_get_main_queue(), ^{
[self.scroller addSubview:menuBtn];
});
Change this
menuBtn=[pccButtonViewController alloc ];
to this
menuBtn=[[pccButtonViewController alloc ] init];
From documenation:
buttonWithType:
This method is a convenience constructor for creating button objects with specific configurations. If you subclass UIButton, this method does not return an instance of your subclass. If you want to create an instance of a specific subclass, you must alloc/init the button directly.