Troubleshooting

I have rejectModalViewControllerAnimated to work correctly on the next setup, but I am confused why it works on self (modalViewController) and not on parentViewController.

Here's the setting:

  • I have a UITableViewController with a navigation button that calls a modal view:
- (void)viewDidLoad { [super viewDidLoad]; self.title = @"Root"; _data = [NSArray arrayWithObjects:@"One", @"Two", nil]; _detailController = [[DetailViewController alloc] init]; // Uncomment the following line to preserve selection between presentations. // self.clearsSelectionOnViewWillAppear = NO; // Uncomment the following line to display an Edit button in the navigation bar for this view controller. // self.navigationItem.rightBarButtonItem = self.editButtonItem; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(showAbout)]; } - (void)showAbout { AboutViewController *abv = [[AboutViewController alloc] init]; abv.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; [self presentModalViewController:abv animated:YES]; } 

Here's a modal AboutViewController view controller with a toolbar button that triggers a reject action to close the modal:

 - (IBAction)dismissAction:(id)sender { [self dismissModalViewControllerAnimated:YES]; } 

My question is why does [self offsetModalViewControllerAnimated] work and not [self.parentViewController rejectModalViewControllerAnimated] ?? Is this new in iOS 5? I thought that only parentViewController is able to reject a child modal view?

Thanks!

+4
source share
2 answers

[self dismissModalViewControllerAnimated:YES]; Worked for quite some time. One of the best secrets in iOS development if you ask me.

However, self.parentViewController does not actually work new to iOS 5. It has been "replaced" by self.presentingViewController .

This causes an interesting problem for code that tries to be compatible with pre-iOS 5. Since, as you discovered, self.parentViewController returns nil on iOS 5. And UIViewControllers does not respond to presentingViewController pre-iOS 5.

This leaves us with something like this:

 if ([self respondsToSelector:@selector(presentingViewController)]){ [self.presentingViewController dismissModalViewControllerAnimated:YES]; } else { [self.parentViewController dismissModalViewControllerAnimated:YES]; } 
+10
source

Instead of using what NJones said, I suggest sticking with

 [self dismissModalViewControllerAnimated:YES] 

The reason why this will work for all OSs is indicated in the documentation itself:

"The presentation view controller is responsible for rejecting the presented presentation controller. If you call this method only on the presented presentation controller, it will automatically forward the message to the presentation controller. "

NOTE. However, a note about this method in iOS5.0. dismissModalViewControllerAnimated : method is deprecated. dismissViewControllerAnimated:completion : should be used here.

+8
source

Source: https://habr.com/ru/post/1390587/


All Articles