[iPhone] Strange behavior with modal view controllers
[iPhone] Strange behavior with modal view controllers
- Subject: [iPhone] Strange behavior with modal view controllers
- From: WT <email@hidden>
- Date: Wed, 17 Jun 2009 12:43:02 +0200
Hi there,
I'll ask the question first, then I'll explain why I'm asking it.
Is there any known reason whatsoever why presentModalViewController
would do absolutely nothing?
In an iPhone game app I'm working on, I have a tab-bar with 5 items, 2
of which are managed by the SettingsViewController and the
GameViewController, respectively. The player can freely switch between
any two views by tapping the appropriate button on the tab-bar,
*except* that - once the game view has been switched into - the player
can only switch between the game view and the settings view. This
allows the player to change settings within the app, but without being
in a game, as well as while playing a game, and prevents the player
from accessing other views that are not supposed to be accessible
during game play.
In order to implement this, I use the tab-bar delegate method -
tabBarController: shouldSelectViewController: to detect when the
player is attempting to switch to the game view, make the game view
controller the active one (directly, not through the tab-bar
controller), release the tab-bar controller, and then return NO:
- (BOOL) tabBarController: (UITabBarController*) tab_bar_controller
shouldSelectViewController: (UIViewController*)
view_controller
{
BaseViewController* curVC = (BaseViewController*)
tabBarController.selectedViewController;
BaseViewController* newVC = (BaseViewController*) view_controller;
NSUInteger kindOfCurVC = curVC.viewControllerKind;
NSUInteger kindOfNewVC = newVC.viewControllerKind;
if (kindOfNewVC == kindOfCurVC)
{
// Since the tab-bar view responds to taps on an already
// selected tab-bar item, and we don't want to do work that
// has already been done, we should return NO in this case.
return NO;
}
else
if (kindOfNewVC == kViewControllerKindGame)
{
// Switching to the game view. Return NO because
// the active view controller will change directly
// to the game view controller and the tab-bar controller
// will never get a chance to switch to the game view.
[tabBarController.view removeFromSuperview];
[window addSubview: gameViewController.view];
[tabBarController release];
tabBarController = nil;
return NO;
}
else
{
// Switching to a view other than the game view.
return YES;
}
}
At this point, it's as if I never had a tab-bar controller. Of course,
I need to have retained both the SettingsViewController and the
GameViewController, which I did when I grabbed them from the tab-bar
view controllers array back in the -applicationDidFinishLaunching:
method.
So, now, when I want to switch between the game and settings views, I
use a modal view controller scheme, with the SettingsViewController
being the modal partner of the GameViewController. I also have a
button on the settings view, that is hidden when the settings view
controller is being managed by the tab-bar but visible when not. The
button's action triggers the dismissal of the modal behavior,
returning to the game view.
Everything works great, except for one thing: the call
[gameViewController presentModalViewController: settingsViewController
animated: YES];
does absolutely nothing! Moreover, I've verified that, after that
call, gameViewController.modalViewController is nil. The AppDelegate
method
- (void) switchFromGameToSettings
{
NSLog(@"AppDelegate: -switchFromGameToSettings");
[gameViewController presentModalViewController:
settingsViewController animated: YES];
NSLog(@"settingsViewController = %@", settingsViewController);
NSLog(@"gameViewController = %@", gameViewController);
NSLog(@"gameViewController.modalViewController = %@",
gameViewController.modalViewController);
// Tell the settings view controller that it is now in-game.
settingsViewController.inGame = YES;
}
(triggered by a tap on the appropriate button on the game view, which
triggers an action method in the game view controller, which calls -
switchFromGameToSettings on the application delegate)
results in:
AppDelegate: -switchFromGameToSettings
settingsViewController = <SettingsViewController: 0xd1b720>
gameViewController = <GameViewController: 0xd1bd40>
gameViewController.modalViewController = (null)
My first thought was that perhaps either one or both of the view
controllers might still be tied up with the tab-bar controller,
somehow, even though I've killed the tab-bar by this point. So, I
tried a little hack where I create a fresh new pair of view
controllers just prior to the presentModalViewController call, and now
things do work correctly.
A little more investigation then revealed that I don't need to have a
fresh new SettingsViewController object, ie, the one I grabbed from
the tab-bar works just fine. The problem is then with the
GameViewController object.
Now, since the game view does appear on screen and since I can, in
fact, tap the "switch to settings" button on it, and I can see that
the action is being triggered right up to the
presentModalViewController call, I am reasonably sure that I activated
the game view controller correctly when I transitioned from the tab-
bar to the game view controller.
So, why isn't presentModalViewController working?
Any help would be greatly appreciated, and apologies for the long
message.
Wagner
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden