Behavior changed for parentViewController in iOS 5
Previously, if we present a modal view controller using:
[self presentModalViewController:modalViewController animated: YES]
We should able to get back the parent of the modal view controller using:
[modelViewController parentViewController]
and normally we will dismiss the view controller using:
[[modelViewController parentViewController] dismissModalViewControllerAnimated:YES];
However, after iOS 5, the behavior of parentViewController is changed, that it will return nil for the modal view controller. Instead, it's parent view controller can be given by the new accessor presentingViewController. i.e. the old parentViewController spited to parentViewController and presentingViewController
To fix the problem, I create the following defines:
#ifndef __presentingViewController
#define __presentingViewController(obj) (([obj respondsToSelector:@selector(presentingViewController)]) ? [obj presentingViewController] : [obj parentViewController] )
#endif //__presentingViewController//
Then we can use:
__presentingViewController(modelViewController)
instead of:
[modelViewController parentViewController]
Actually the dismiss a modal dialog, instead of:
[[modelViewController parentViewController] dismissModalViewControllerAnimated:YES];
we can simply called:
[modelViewController dismissModalViewControllerAnimated:YES];
so that we can avoid calling parentViewController in many cases.

最新回應
32 周 3 天前
37 周 5 天前
41 周 1 天前
42 周 1 天前
50 周 6 天前
50 周 6 天前
51 周 17 小時前
51 周 2 天前
1 年 6 天前
1 年 1 周前