作者:不及格的程式員-八神
記憶體管理在iOS程式開發扮演非常重要角色,對這塊處理不好的程式會在實際裝置上發生Crash情況.
對於經常在視圖控制器中用retain聲名的輸出口一定要注意,在dealloc中release它們.
Obj-C聲明的屬性,具有如下特性,將新對象賦值當前屬性,並自動釋放屬性中的舊對象.
- 在appDelegate.m中如果有一個window輸出口,或者另外加了一個導航視圖控制器或是其它什麼視圖控制器時,在 dealloc 方法中,執行這些輸出口的release方法.而不必在applicationDidFinishLaunching中執行釋放.
- 在單獨的自訂視圖控制器中,如果在接到記憶體緊缺通知時,會執行didReceiveMemoryWarning方法,它會卸載當前視圖表單,並執行viewDidUnload方法,所以在該方法中將方視圖中的定義的輸出口置為nil. 同時在該視圖的dealloc方法(當該視圖控制項器的release被調用時,該方法被執行)中執行相應的輸出口release方法.
-(void)viewDidUnload
{
self.** = nil;
}-(void)dealloc
{
[** release];
[supper dealloc];
}
- 執行個體方法,靜態方法,Factory 方法返回的對象的釋方規則,後兩者都不必手動執行release方法,由系統的記憶體池自動維護.
- 如果我在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];
- 在自訂視圖控制器中聲明了變數,但是沒定義相應屬性,這時對象釋放規則同第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
- 在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
-
都在dealloc中執行.我看到在wordPress的iPhone用戶端這樣做的,不過好像它在特意相容iOS3之前的版本,3.0之前的版本並不存在viewDidUnload方法,蘋果的開發手冊上有描述.
-(void)dealloc
{
[myView release], myView = nil;
}附.
python /Users/yangiori/Downloads/facebook-three20-309d34e/src/scripts/ttmodule.py -p /Users/yangiori/Documents/iPhoneLiaoBao/iPhoneLiaoBao.xcodeproj Three20 //區分大小寫。。。