小記 iOS 視圖控制器的記憶體申請與釋放普遍規則

來源:互聯網
上載者:User

作者:不及格的程式員-八神

記憶體管理在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. 執行個體方法,靜態方法,Factory 方法返回的對象的釋方規則,後兩者都不必手動執行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];
  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;
    }

    附.
    python /Users/yangiori/Downloads/facebook-three20-309d34e/src/scripts/ttmodule.py -p /Users/yangiori/Documents/iPhoneLiaoBao/iPhoneLiaoBao.xcodeproj Three20 //區分大小寫。。。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.