Search code examples
iphoneiosipadios5automatic-ref-counting

pushing viewcontroller using ARC?


I have two scenario for the project with ARC and project without ARC.

1) The project without ARC.we can use the following.

 MyViewController* viewController = [[MyViewController alloc] init];
 [self.navigationController pushViewController:viewController animated:YES];
 [viewController release];

2)How can I achieve the above in the project which is with ARC.

      a)where can I allocate memory?
      b)where can I release viewcontroller after pushing?
      c)is there any standard for it?

Solution

  • When using ARC you do not need to release the viewController, the compiler will add the release and retain for you..

    So in ARC this will do:

    MyViewController* viewController = [[MyViewController alloc] init];
    [self.navigationController pushViewController:viewController animated:YES];
    

    Using retain, release and autorelease will result in a compiler error.


    Be aware that you will need to use the @property correctly when using ARC. Use strong for properties that you want to retain and weak for properties that you just want to assign. If you want iOS 4.3 support you can't use weak but should use unsafe_unretained.