UINavigationController not working under ARC in iPhone

Posted by user1811427 on Stack Overflow See other posts from Stack Overflow or by user1811427
Published on 2012-12-19T04:53:31Z Indexed on 2012/12/19 5:03 UTC
Read the original article Hit count: 234

I have creared a new project "Empty Application" template in Xcode 4.3, it is having only two classes AppDelegate.h & .m

I cheaked with ARC to use automatic reference count while creating the app.

I added two new files "RootViewController" & "NewProjectViewControllers".

I implemented code to set navigation controller as follows in AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
      self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
      // Override point for customization after application launch.

      rootViewController = [[MainViewController alloc] init];
      UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:rootViewController];
      [self.window addSubview:navigation.view];

      self.window.backgroundColor = [UIColor whiteColor];
      [self.window makeKeyAndVisible];
      return YES;
}

and in hte home view (Root view controller) implemented as follows

- (void)viewDidLoad
{
      [super viewDidLoad];

      self.title = @"Projects";
      UINavigationBar *navigationBar = [self.navigationController navigationBar];
      [navigationBar setTintColor:   [UIColor colorWithRed:10/255.0f green:21/255.0f blue:51/255.0f alpha:1.0f]];     

      //To set the customised bar item
      UIButton *rightBarBtn = [UIButton buttonWithType:UIButtonTypeCustom];
      [rightBarBtn setBackgroundImage:[UIImage imageNamed:@"plus_new.png"] forState:UIControlStateNormal];
      rightBarBtn.frame=CGRectMake(0.0, 100.0, 30.0, 30.0);
      [rightBarBtn addTarget:self action:@selector(addProject)  forControlEvents:UIControlEventTouchUpInside];

      UIBarButtonItem* rightBarItem = [[UIBarButtonItem alloc] initWithCustomView:rightBarBtn];
      self.navigationItem.rightBarButtonItem = rightBarItem;

      // Do any additional setup after loading the view from its nib.
}

- (void) addProject
{
      NewProjViewController *editProject = [[NewProjViewController alloc] init];
      [self.navigationController pushViewController:editProject animated:YES];

      NSLog(@"xxxxxxxxxxxxxxx");
}

But since i used ARC the navigation may dealoc immediately and it doesn't work, All the actions in method works except push to the next view

if i do same thing with out ARC it works fine

How to resolve this issue..? Thanks in advance

© Stack Overflow or respective owner

Related posts about iphone

Related posts about uinavigationcontroller