Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mrob44

macrumors member
Original poster
Feb 7, 2009
77
14
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

macrumors 6502
Dec 22, 2009
471
1
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.

Code:
- (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

macrumors member
Original poster
Feb 7, 2009
77
14
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.

Code:
- (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

macrumors 6502
Dec 22, 2009
471
1
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.

Code:
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.