开发者

Nested ViewControllers and their memory imprint? Improvements if this is very bad?

@Jonah pointed me to the following explanation he has provided: Question on View Controller's view

I currently have a MainViewController whose main(root?) view contains an Add Record UIButton which is linked to the following method and is dependent on which page the user currently has open in a UIScrollView:

#pragma mark -
#pragma mark Add View Support

- (void)add:(id)sender {

MyAddViewController *controller = nil;

    // Dynamically creating and allocating the Add View Controller
if (dataViewPageControl.currentPage == 0) {
    controller = [[MyAddViewController alloc] initWithAddType:1];
} else {
    controller = [[MyAddViewController alloc] initWithAddType:0];
}

controller.delegate = self;

    // Slide the new AddViewController's view in from the right
    CGPoint initialCenter = self.view.center;
controller.view.center = CGPointMake(initialCenter.x + 320, initialCenter.y);
[self.view addSubview:controller.view];

[UIView beginAnimations:@"animation" context:NULL];
[UIView setAnimationDuration:0.35];
[UIView setAnimationDelegate:controller];
controller.view.center = CGPointMake(initialCenter.x, initialCenter.y);
[UIView commitAnimations];

}

MainViewController is the delegate for MyAddViewController (which has a CANCEL/CLOSE button) and when it's close button is clicked the MyAddViewController is released through the close method in this MainViewController.

As you can see, MyAddViewController's view becomes a subview to the current root view (Bad? Bad? Bad?) MyAddViewController's view is as a FULL size view (320x416) but whose top half is transparent, so that MainViewController's view can be seen below on the top half and the bottom half contains the AddViewController's view and textfields etc....

Based on what I've read from @Jonah just now at the link at the very top this sort of design is BAD, BAD, BAD. I've noticed when testing in Instruments that MyAddViewController sometimes lags when sliding in. I'll click the ADD button to fire this method and there is sometimes a 3 - 5 sec delay before the view slides in. Some sort of delay 开发者_StackOverflowin all the memory management and cleanup?

I'm assuming I can fix this by making a single MyAddViewController property inside MainViewController which will then prevent me from allocating and deallocating on every button click. That should make things smoother? My only worry is that this will cause a greater memory imprint at application load time. Instruments seems to show a 516KB allocation every time I click ADD for the first time in my app.

Any recommendations though on design and how to improve this? The reason I like having separate view controllers with nested views like this is because MyAddViewController has a lot of logic with Core Data and such and MainViewController is pretty busy enough already as it is. Trying to keep things modular.

Thanks in advance!


I think the best solution is to redesign your MyAddViewController to be a controller which inherits from NSObject rather than UIViewController. The MyAddViewController can then manage a subview of your MainViewController allowing you to keep your controller logic nicely encapsulated without abusing UIViewController.

I've tried to describe why nesting the views of multiple custom UIViewController classes is problematic here: http://blog.carbonfive.com/2011/03/09/abusing-uiviewcontrollers/


An add view controller is the perfect type of thing to use modal presentation for. Instead of handling all that animation yourself, just present it like this:

[self presentModalViewController:controller animated:YES];

Edit

Then I would recommend making MyAddViewController a property on MainViewController and write a custom accessor. Additionally I would defer setting it's addType (if possible) until the point at which you will actually animate the view in. This way we can just have one generic MyAddViewController hanging around, instead of having to create a new one every time the detailPageControl changes.

- (MyAddViewController *)addViewController
{
    if (!addViewController)
        addViewController = [[MyAddViewController alloc] init];
}

Then before you animate it...

self.addViewController.addType = 0;

if (dataViewPageControl.currentPage == 0) {
    self.addViewController.addType = 1;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜