0

I have a very simple ViewController that contains the following:

@interface ServingsView : UIViewController
@property (retain, nonatomic) IBOutlet UILabel *labelTitle;
@property (retain, nonatomic) IBOutlet UILabel *labelContent;
@property (retain, nonatomic) IBOutlet UIButton *buttonSelect;

I added no code to the m file of this controller.

Now, I'm creating this viewcontroller and add its view to a scroll view:

 for (NSSubArray * Choice in currentItem.ItemsArray)
    {
        stView * ChoiceView=[[stView alloc]initWithNibName:@"stView" bundle:nil];
        ChoiceView.view.tag=1515;
        [mScrollView addSubview:ChoiceView.view];
        ChoiceView.view.frame=CGRectMake(0, [self getMinimumHeight]+h*ChoiceView.view.frame.size.height , 320, ChoiceView.view.frame.size.height);
        ChoiceView.labelTitle.text=Choice.ArrayName;

        [ChoiceView.buttonSelect addTarget:self action:@selector(onSubservingItemClicked:) forControlEvents:UIControlEventTouchUpInside];

        ChoiceView.buttonSelect.tag=h;
        h++;
        increaseHeight+=ChoiceView.view.frame.size.height;
        // here is the problem:
        [ChoiceView release];
    }

now, I'm not using ChoiceView anywhere afterwords. Whats going on is, that the button is not even shown and that view has no response what so ever. when using the views in its environment to scroll I'm getting different kind of exceptions. sometimes its CALayer exception, sometimes stView exception. when I'm removing the release line, everything is working fine.

I have even created a test project it didn't happen there, so I'm missing something here.

donald
  • 489
  • 5
  • 13

2 Answers2

0

Don't do things like [mScrollView addSubview:ChoiceView.view];. If you look at the Apple video on view controller containment, you'll see that they describe it as an inconsistent view hierarchy: https://developer.apple.com/videos/wwdc/2011/?id=102

Either make the view part of the view hierarchy for the scroll view as it's defined or else create a container view controller that uses the provided parent/child methods.

Phillip Mills
  • 30,888
  • 4
  • 42
  • 57
  • TY for your response! This is a 4.3 app, so I can't use that API. Also, I never get UIViewControllerHierarchyInconsistency as it mentioned there and this code is working on sample project that I did to check it... Also, I don't see any other way to add sub views to a scrollview, since they are dynamically created and I can't tell how many will be. – donald Aug 06 '12 at 07:50
  • If you're back at 4.3, you may need to make your own equivalent of the container controller by creating an array of sub-controllers that holds the view owners. The other fairly safe approach is to design custom views that don't need controllers and load them directly. (Note: It **is** possible to have an inconsistent hierarchy without triggering an event that gives that specific error.) – Phillip Mills Aug 06 '12 at 12:02
  • OK, so I've created an array to hold the subviews controllers, and now I'm getting a consistent error when removing them from the array:[CALayer release]: message sent to deallocated instance. A little progress there.... – donald Aug 06 '12 at 14:07
0

OK, this is the very strange answer! After many tests and a mock up application that failed to raise this error, I added the faulty controller to another application. Got the same results! So, I have deleted it entirely and create a new one... Now it's working.

Apple - go figure...

donald
  • 489
  • 5
  • 13