I have a lot of problems dynamically adding subviews to UIScrollView. Scrolling view works fine with content created in NIB, but since the displayed nested objects depend on the data in my application (a mixture of images, labels, radio buttons, etc.), I should be able to create and display them dynamically.
According to everything I read, it looks pretty simple on different sites and in the Apple documentation. In the view controller viewDidLoad, I added the following code,
UILabel *testLabel = [[UILabel alloc] init]; [testLabel setFrame:CGRectMake(50, 50, 100, 40)]; [testLabel setText:@"My Test label"]; [scrollView addSubview:testLabel]; [testLabel release];
The label will not appear in the scroll view, but if I add testLabel to self.view, it will appear (but not in the scrollable content). I even tried to add code to viewDidAppear in case I misunderstood the order of events without any luck.
When I checked the debugger, I noticed that the scroll address is 0x0, which I suppose for some reason means its zero, which explains why it doesn't work. I was on the assumption that if I linked this scrollView pointer to the actual scroll in IB, it would be automatically assigned the correct address. This is not true? If so, how do I get the address of the submission?
- UPDATE -
Thanks everyone for the feedback. I checked everything, as everyone suggested, and it was, of course, everything was right. I did not need to set the size of the content, since I had some other dummy labels (for checking the scroll operation) in the NIB. But I will remember this later :-)
Interestingly, by checking the code again and not making any changes, I ran it again and it just worked! Not sure why, but I will post the reason if I ever find out ...