简体   繁体   中英

ios multiple views with a scroll view deallocated instance

I am have an issue with my scrollview which holds multiple views. I think the problem is that subviews are being released. I have buttons in the subviews and when I click the buttons I get this error, [GraphDisplayViewController performSelector:withObject:withObject:]:

message sent to deallocated instance

. If there is only one subview then I can just use a property and this works, but since the number of subviews varies(one or more), it does not work and I don't know how to solve this.

I currently load all the views at once in the beginning. I'm working on only loading one subview at a time and assigning the property to that view, but I'm not sure if that will work.

My layout is as follows, a parent view(DetailViewController) contains a scrollview, I add views(GraphDisplayViewController) to the scrollview, the subviews each load a view(GraphView).

Any help or advice would be greatly appreciated. If you need any more details please let me know. Thank you for your time.

Code sample of how I add the subviews,

DetailViewController

- (void)loadScrollViewWithPage:(int)page
{
if (page < 0) return;
if (page >= pageControl.numberOfPages) return;

subView = [viewControllers objectAtIndex:page];   
NSString *description;
NSString *packsize;
if ((NSNull *)subView == [NSNull null]) 
{
    subView = [[GraphDisplayViewController alloc] initWithNibName:@"GraphDisplayViewController" bundle:nil];
    [viewControllers replaceObjectAtIndex:page withObject:subView];
    subView = [[GraphDisplayViewController alloc] init];
    subView.molecule = moleculeName;
    subView.description = description;    
    subView.dataArray = moleculePrices;

}
else
{
    return;
}

// add the controller's view to the scroll view
if (nil == subView.view.superview) 
{
    CGRect frame = scrollView.frame;
    frame.origin.x = frame.size.width * page;
    frame.origin.y = 0;
    subView.view.frame = frame;
    [scrollView addSubview:subView.view];
} 
}

UPDATE There was a mistake in the code, testing to see if that solves anything

subView = [[GraphDisplayViewController alloc] initWithNibName:@"GraphDisplayViewController" bundle:nil];
[viewControllers replaceObjectAtIndex:page withObject:subView];
subView = [[GraphDisplayViewController alloc] init]; <- Mistake

you are not retaining the GraphDisplayViewController assigned to subView variable. and hence you are loosing it at some point of time.

as we can see you are fetching the subView object like subView = [viewControllers objectAtIndex:page]; then you should also store it in viewControllers (though i am not sure what logic you have implemented but in ur code for each allocated subView this must be executed : [viewControllers replaceObjectAtIndex:page withObject:subView]; and you need to be sure that viewControllers variable is also retained for everything to work smoothly.)

array so that it can be retained to avoid the crash you are facing.
I hope this will work for you..best of luck

There was a simple error in my code, I init the view twice.

subView = [[GraphDisplayViewController alloc] initWithNibName:@"GraphDisplayViewController" bundle:nil];
subView = [[GraphDisplayViewController alloc] init]; <- Mistake

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM