Reply to comment

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.

Reply

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.