开发者

Retain UIViewController or not in navigation stack

I have a scenario that I'm not sure whether to retain a view contro开发者_Python百科ller or not. Say I had ViewControllerOne. When a button was pressed, ViewControllerOne would push ViewControllerTwo in the navigation stack. ViewControllerTwo needs a reference to ViewControllerOne because it needs to access an ivar in that view controller. The question is, would I retain ViewControllerOne in ViewControllerTwo (@property (retain) ViewControllerOne *vc) or not? The navigation controller already retains the ViewControllerOne so I'm not really sure.

Thanks


You should probably set viewControllerOne up as a delegate of viewControllerTwo. In general, a delegate is not retained.

As you have described it, viewControllerTwo does not need to know anything about viewControllerOne in particular, it just needs to know how to access a piece of data and inform a higher level object of changes to that data. That is a common role for a delegate.


The question is: Why does ViewControllerTwo need to access an ivar from ViewControllerOne ?

The better approach would be to pass the value of the ivar in ViewControllerOne to ViewControllerTwo before pushing ViewControllerTwo to the stack.


Ranpraygo is correct.

The best solution is to add a property to ViewControllerTwo and pass the iVar from ViewControllerOne to it before pushing it to the navigation stack.

By convention the property added to ViewControllerTwo should be retained if it is an object although you could get away with an assigned property in this example because the ViewControllerTwo will be released before ViewControllerOne is and I assume ViewControllerOne has retained the iVar

In objectiveC all variables that are objects are passed by reference primitives and structs are passed by value as they would be in C

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜