PDA

View Full Version : dismissModalViewControllerAnimated:YES -- Issue with SplitView




mrob44
Jul 21, 2010, 06:47 PM
I'm working on an iPad app that is using the SplitView Template provided by Xcode.

On the DetailViewController when a user selects an item from a table, I'm using PresentModalViewController to display another view, and then on that view I have a back button which calls dismissModalViewController to go back to the DetailView.

This works fine in Landscape View, and technically "works" in Portrait View as well, but in Portrait Mode, the button for the PopOver in the upper left corner of the toolbar in DetailView will move itself to the right a few pixels at a time, every single time I call and then dismiss the view, eventually walking itself all the way across the toolbar.

Has anybody seen this or have any thoughts on how to fix this. It seems extremely odd.

Thanks in advance.

****EDIT****

Turns out in Landscape View things don't work as well as I thought. If i start in Portrait View, present and then dismiss the ModalView, when I rotate to Landscape View the PopOver Button is still on the toolbar in the DetailView, and if I click it, the side tableview (RootView) disappears and leaves a black hole.



seepel
Jul 22, 2010, 05:08 AM
I've definitely had problems with this. I think this is essentially a problem that UISplitViewController doesn't differentiatie between hiding and showing the RootViewController when the user rotates the iPad and when there is a modal view. I solved by this, simply by checking to see if the button is already there. Here's the code in my UISplitViewControllerDelegate that seems to work for me, hopefully it is clear enough, and helps with your problem.


- (void)splitViewController:(UISplitViewController*)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem*)barButtonItem forPopoverController:(UIPopoverController*)pc {
NSMutableArray* items = [self.toolbar.items mutableCopy];
if(items == nil) items = [NSMutableArray arrayWithCapacity:1];
if(items.count > 0) {
if(barButtonItem == [items objectAtIndex:0])
return;
[items insertObject:barButtonItem atIndex:0];
self.toolbar.items = items;
}

- (void)splitViewController:(UISplitViewController*)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem {
NSMutableArray* items = [self.toolbar.items mutableCopy];
[items removeObject:barButtonItem];
self.toolbar.items = items;
}

mrob44
Jul 22, 2010, 11:00 AM
I've definitely had problems with this. I think this is essentially a problem that UISplitViewController doesn't differentiatie between hiding and showing the RootViewController when the user rotates the iPad and when there is a modal view. I solved by this, simply by checking to see if the button is already there. Here's the code in my UISplitViewControllerDelegate that seems to work for me, hopefully it is clear enough, and helps with your problem.


- (void)splitViewController:(UISplitViewController*)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem*)barButtonItem forPopoverController:(UIPopoverController*)pc {
NSMutableArray* items = [self.toolbar.items mutableCopy];
if(items == nil) items = [NSMutableArray arrayWithCapacity:1];
if(items.count > 0) {
if(barButtonItem == [items objectAtIndex:0])
return;
[items insertObject:barButtonItem atIndex:0];
self.toolbar.items = items;
}

- (void)splitViewController:(UISplitViewController*)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem {
NSMutableArray* items = [self.toolbar.items mutableCopy];
[items removeObject:barButtonItem];
self.toolbar.items = items;
}



Thanks so much for this. I'm not entirely sure I understand why exactly it fixes the issue, but it definitely solved my problem. Thanks again.

seepel
Jul 22, 2010, 02:53 PM
Thanks so much for this. I'm not entirely sure I understand why exactly it fixes the issue, but it definitely solved my problem. Thanks again.

It's simple really, when the root view becomes visible, it just checks to make sure that it doesn't already have a button for it. Here I am assuming that the button is the first button in the toolbar. If the button already exists, the function returns without doing anything.


if( barButton == [items objectAtIndex:0])
return;


If you blindly add the button, and you are hiding and showing the view willy nilly, there are circumstances where it will just keep adding the button over and over again. Then you suddenly have multiple buttons.