开发者

Bringing up new view controllers - releasing query

I've written some code where I bring up a new view (from my main view controller); then it calls the main controller when it is closed, like so -

-(void)showMyNewView {

    MyNewViewController *myNewViewController = [[MyNewViewController alloc] initWithNibName:@"MyNewViewController" delegate:self];

    [self.view addSubview:myNewViewController.view];

}

and then wh开发者_如何学运维en the new one closes, it calls -

-(void)myNewViewControllerDidFinish:(MyNewViewController *)myNewViewController {

    [myNewViewController.view removeFromSuperview];

    [myNewViewController release];

}

Now this works fine, and there are no leaks, but the compiler moans with warnings about "Potential leak of an object allocated on line x and stored into myNewViewController".

I've been looking at Apple's presentModalViewController:animated: code, which also doesn't release the new modal view controller in the method which creates it, it seems to release it with a dismissModalViewControllerAnimated: call when the delegate's viewControllerDidFinish: method is called. Is there something I'm missing here? Using the presentModalViewController code doesn't generate any warnings. Many thanks for any help.


I think I've figured it out now, and I've written a small bit of code which gives me my own version of "presentModalViewController:animated:" with all the control I want. I'd be grateful to hear what more seasoned coders make of this (it's probably really straight forward but I've not been doing this for very long...), and if there are any problems with the code, etc -

Interface:

#import <UIKit/UIKit.h>


enum {
    MyViewLoaderTransitionTypeNone = 0,
    MyViewLoaderTransitionTypeSomeEffect,
    MyViewLoaderTransitionTypeSomeOtherEffect
};
typedef NSInteger MyViewLoaderTransitionType;


@interface MyViewLoader : UIViewController {

    UIViewController *myLoadedViewController;

}

@property (nonatomic, retain) UIViewController *myLoadedViewController;

-(void)loadView:(UIViewController *)theViewController withTransition:(MyViewLoaderTransitionType)theTransition;
-(void)dismissViewWithTransition:(MyViewLoaderTransitionType)theTransition;

@end

Implementation:

#import "MyViewLoader.h"


@implementation MyViewLoader

@synthesize myLoadedViewController;


-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        ;
    }

    return self;

}


-(void)dealloc {

    [myLoadedViewController release];

    [super dealloc];

}


-(void)loadView:(UIViewController *)theViewController withTransition:(MyViewLoaderTransitionType)theTransition {

    [self setLoadedViewController:theViewController];

    UIView *theLoadedView = theViewController.view;
    [self.view addSubview:theLoadedView];

    // do all sorts of transition stuff here

    [theViewController viewWillAppear:NO];

}


-(void)dismissViewWithTransition:(MyViewLoaderTransitionType)theTransition {

    UIView *theLoadedView = self.loadedViewController.view;

    // do all sorts of transition stuff here

    [theLoadedView removeFromSuperview];
    self.loadedViewController = nil

}

I just use MyViewLoader as the superclass of any view controllers where I need it.

Thanks for any comments / help!


The usual thing to do here is, when you add a subview to a view, release the subview directly after. The parent view becomes responsible for the subview. When removeFromSuperview is called later, that decrements the retain count and the subview is automatically released.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜