iOS程序开发扮演非常重要角色,对这块处理不好的程序会在实际设备上发生Crash情况.

对于经常在视图控制器中用retain声名的输出口一定要注意,在dealloc中release它们.


Obj-C声明的属性,具有如下特性,将新对象赋值当前属性,并自动释放属性中的旧对象.


1.在appDelegate.m中如果有一个window输出口,或者另外加了一个导航视图控制器或是其它什么视图控制器时,在 dealloc 方法中,执行这些输出口的release方法.而不必在applicationDidFinishLaunching中执行释放.


2.在单独的自定义视图控制器中,如果在接到内存紧缺通知时,会执行didReceiveMemoryWarning方法,它会卸载当前视图窗体,并执行viewDidUnload方法,所以在该方法中将方视图中的定义的输出口置为nil. 同时在该视图的dealloc方法(当该视图的release被调用时,该方法被执行)中执行相应的输出口release方法.  


-(void)viewDidUnload


{


    self.** = nil;


}


-(void)dealloc


{


    [** release];


    [supper dealloc];


}


3.实例方法,静态方法,获取方法返回的对象的释方规则,后两者都不必手动执行release方法,由系统的内存池自动维护.


4.如果我在appDelegate.m文件中alloc并init一个视图控制器,然后把它通过导航控制器的initWithRootViewController方法加到其中时,那么加完之后要释放那个视图
.Example:  

myController = [[*** alloc] initWithNibName:"***" bundle:nil];


UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:myController];


[myController release];


[tabController presentModalViewController:navi animated:YES];


[navi release];


-(void) dealloc


{


    [myController release]


    [tabController release];


    [supper dealloc];


}



5.在自定义视图控制器中声明了变量,但是没定义相应属性,这时对象释放规则同第1条相同.

@interface *** : UIViewController


{


    UIView *_view;


}


@end


@implementation ***


-(void)loadView


{


    [supper loadView];


    _view = [[UIView alloc] initWithFrame:[self.view bounds]];


    [self.view addSubview:_view];


}


-(void)dealloc


{   


    [_view release];


    [supper dealloc];


}


@end



6.在iPhone3开发基础教程中的样例切换视图的代码问题,我觉得里面有一个顺序写错了,之后我在google查了一下,有不少人有同问.

@implementation SwitchViewController


@synthesize blueViewController;


@synthesize yellowViewController;


- (void)viewDidLoad


{


    BlueViewController *blueController = [[BlueViewController alloc] initWithNibName:@"BlueView" bundle:nil];


    self.blueViewController = blueController;


    [self.view insertSubview:blueController.view atIndex:0];


    [blueController release];


}


- (IBAction)switchViews:(id)sender


{


    [UIView beginAnimations:@"View Flip" context:nil];


    [UIView setAnimationDuration:1.25];


    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];


    if (self.yellowViewController.view.superview == nil)//这条语句是最有趣的,OBJ-C允许向nil发送消息.


    {


        if (self.yellowViewController == nil)


        {


            YellowViewController *yellowController =


            [[YellowViewController alloc] initWithNibName:@"YellowView"


                                                   bundle:nil];


            self.yellowViewController = yellowController;


            [yellowController release];


        }


        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];


        [blueViewController viewWillAppear:YES];


        [yellowViewController viewWillDisappear:YES];


        [blueViewController.view removeFromSuperview];


        [self.view insertSubview:yellowViewController.view atIndex:0];


        [yellowViewController viewDidDisappear:YES];


        [blueViewController viewDidAppear:YES];


    }


    else


    {


        if (self.blueViewController == nil)


        {


            BlueViewController *blueController =


            [[BlueViewController alloc] initWithNibName:@"BlueView"


                                                 bundle:nil];


            self.blueViewController = blueController;


            [blueController release];


        }


        //[UIView setAnimationTransition:


        // UIViewAnimationTransitionFlipFromLeft


        //                       forView:self.view cache:YES];


        //[yellowViewController viewWillAppear:YES];


        //[blueViewController viewWillDisappear:YES];


        [yellowViewController.view removeFromSuperview];


        [self.view insertSubview:blueViewController.view atIndex:0];


        //[blueViewController viewDidDisappear:YES];


        //[yellowViewController viewDidAppear:YES];


    }


    //[UIView commitAnimations];


}


- (void)didReceiveMemoryWarning


{


    [super didReceiveMemoryWarning];


    if (self.blueViewController.view.superview == nil)


        self.blueViewController = nil;


    else


        self.yellowViewController = nil;


}


- (void)dealloc


{


    [yellowViewController release];


    [blueViewController release];


    [super dealloc];


}


@end



7.  都在dealloc中执行.我看到在wordPress的iPhone客户端这样做的,不过好像它在特意兼容iOS3之前的版本,3.0之前的版本并不存在viewDidUnload方法,苹果的开发手册上有描述.    

-(void)dealloc


{


    [myView release], myView = nil;


}