问题
I have an iOS application that I'm just starting to implement game center support for. I call the authenticateWithCompletionHandler method during loadView for my main (root) view controller. My main view displays, and shortly later the "sign in to game center" dialog appears exactly as expected. If "Create New Account" is selected, the game center "New Account" pop-over dialog displays in the middle of the screen (this is on an iPad) -- but my underlying main menu disappears. Underneath the pop-over dialog is just a black screen. If I dismiss the dialog in the completion handler, it goes away as expected and my main menu comes back.
I figure this must be some sort of view hierarchy issue, and so to try to simplify the issue I created a new project. Using Xcode 4.3.2 I modified a new "Empty Application" iOS project by adding a single view controller class, "AppViewController". I changed the system created AppDelegate.m file applicationDidFinishLaunchingWithOptions method to set the rootViewController (right after the window background color is set to white):
self.window.rootViewController =
[[[AppViewController alloc] initWithNibName:nil bundle:nil] autorelease];
...and I set the loadView method of AppViewController to load a view with a colored background (so I could tell if it was properly compositing on top of it) and then call the gamekit authentication method:
- (void) loadView {
self.view = [[[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame] autorelease];
self.view.backgroundColor = [UIColor yellowColor];
GKLocalPlayer * localPlayer = [GKLocalPlayer localPlayer];
[localPlayer authenticateWithCompletionHandler:nil];
}
This simple structure exactly mimics how I have my view hierarchy set up -- a single UIViewController set as the window rootViewController which loads and manages a view heirarchy. No XIB files.
Anyway, I get exactly the same behavior with this very simple test app. When the "New Account" pop-over dialog appears, my underlying view (in this case a yellow fill) goes away (and the underlying window white fill shows). I assume this has to do with my misunderstanding of how iOS wants me to manage these controllers and views.
However, what is particularly odd (at least to me) is if I either cancel out of the "New Account" window or log in using an existing account and then immediately bring up the achievement pop-over dialog (using presentModalViewController), that works! I can see my main menu view behind the achievement dialog. Adding a simple button to the view and button press handler to the view controller showed the same thing -- the achievements dialog shows the background view just fine, but "New Account" dialog obscures the underlying view. Obviously, I would like the "New Account" dialog to look like its floating on top of my underlying view. I should mention I'm testing on iOS 5.1.
Thanks for any help you might be able to give me!
UPDATE: After taking the advice of another stackoverflow post I purchased "Beginning iOS 5 Development" by Jeff LaMarche, David Mark, and Jack Nutting, and reviewed the section about starting from the Xcode "Empty Application" template. This led me to modify my test code slightly. Instead of setting the "rootViewController" property directly, I am now doing this in applicationDidFinishLaunchingWithOptions:
UIViewController * viewController =
[[[AppViewController alloc] initWithNibName:nil bundle:nil] autorelease];
viewController.view.backgroundColor = [UIColor yellowColor];
[self.window addSubview:viewController.view];
I also got rid of the above loadView method on my view controller. This works! Does anyone have any idea why? I don't see a substantive difference between the two systems. In the first I guess I am recreating the view on the view controller (there is no alloc/init on the view controller view in the second system); is there something special about the view that is automatically created?
UPDATE x2: OK, it looks like the real difference is in the line:
[self.window addSubview:viewController.view];
I thought setting the rootViewController on the window would automatically add the view to the window, but unless I do that explicitly, I get this odd behavior. So I have found a work around for this issue, I just don't understand why it works. Any ideas? Thanks!
回答1:
I'm not sure what is causing your problem, but the authenticateWithCompletionHandler method is usually called as soon as your app starts. Try moving it inside applicationDidFinishLaunchingWithOptions method in your appDelegate and see if it works then.
来源:https://stackoverflow.com/questions/10561309/underlying-view-disappears-during-game-center-authentication