开发者

UINavigationController strategy (iPhone)

Something is puzzling me, after going through my app with instruments, it is a UINavigationBased App, it noticed this.

Each time a tableView cell is tapped and I do this:

        GenericTableViewController *someViewController = [[Generic TableViewController alloc] init];    
        [self.navigationController pushViewController:someViewController animated:YES];
        [someViewController release];

I then tap the back button and look in Instruments to see how many living instances of the GenericTableViewController there exists. There is 1 before the back tap, and 0 afterwards. This is great, good clean memory management by the UINavigationController.

However, other places in the app I don't use the back button to leave a view, in some viewControllers tapping one of my custom buttons wil开发者_开发技巧l run code completely like the above, but the viewController, in which I tapped the custom button, will not be released.

I guess I understand why, the navigationController makes a judgment call, that I might be pushing something on to the stack and that I will return later, so it keeps the controller in its memory. This must mean that tapping the back button uses a [self.navigationController popViewController]. Ok.

The problem is now that each time I visit a view that isn't left using a "back" button in the navigationBar, it will not be released. The count allocation in Instruments just goes up each time I visit a view, until I have like 20 objects living.

I tried putting a [self.navigationController popViewController] in the selector for my custom buttons. Meaning that I manually pop the visible viewController before pushing a new on to the stack. But the allocation count does not go down? guess the "back" button does not use this technic after all...

I also tried using popToViewController, since I always know if the controller has been on the stack or not. But to use the popToViewController, I need to keep a reference around for the view.

Is there some sound way of saying to the UINavigationController: If this viewController is on your stack, please display it, if not please instantiate it, push to the stack and display it.

The challenge here is probably that not all my navigation in the NavigationController is completely linear. I will sometimes have to go from stack item no. 2 to stack item no. 5, back to 3 etc.

So is there a way to do this, while still having the UINavigationController making sure to release my viewControllers and not instantiating the same viewController several times?


You could try to access UINavigationController's viewControllers property to get a NSArray of all the view controllers currently on the navigation stack. Then use containsObject: to check if the view controller is already on the stack.
If it is, then use popToViewController:animated: to show the view controller. If it is not on the stack then just use pushViewController:animated:. If this does not help you, maybe setViewControllers:animated: is the right method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜