As in the title, I do not manage to have that callback called in order to dismiss the game center view controller neither on my iOS 7 iPhone nor iOS 8 iPad. This is the code I use:
GKGameCenterViewController *controller=nil;
- (IBAction)achievementButtonClicked:(id)sender {
if (!controller){
controller=[[GKGameCenterViewController alloc] init];
controller.delegate=self;
}
NSLog(#"controller=%#", controller);
if (controller) [self presentViewController:controller animated:YES completion:nil];
}
-(void)gameCenterViewControllerDidFinish:(GKGameCenterViewController *)gameCenterViewController{
[gameCenterViewController dismissViewControllerAnimated:YES completion:nil];
// I also tried [self dismissViewControllerAnimated:YES completion:nil] but anyway the function seems to not even enter here
}
If I take the function off, the delegate complaints it is missing, so the issue should not be connected to that. What might that be and how to fix it?
My problem is that I used:
controller.delegate=self;
omitting:
controller.gameCenterDelegate = self;
Once inserted the latter, the view controller dismisses without problems, both when I manually present the controller and when it is shown to login her. I really wonder why that beast has too delegates, if not to confuse developers...
Related
EDIT Problem reinstated from scratch.
I have a ViewController A which has a Navigation Bar Button which presents an UIImagePickerController of sourcetype UIImagePickerControllerSourceTypeCamera, we'll call this ViewController B. In the didFinishPickingMediaWithInfo: method of A, I then present ViewController C.
The problem now starts here. When I finally reach C, we can see that view stack is clearly:
A -modal-> B -modal-> C
From C I then have a "Back" button present on the navigation bar which should take me back to B. However, since B is an UIImagePickerController, I cannot reuse it and it must be dismissed. So, to make this happen, I currently have the following method executing for that "Back" button on C:
- (IBAction)backOnPost:(UIBarButtonItem *)sender
{
[self.view endEditing:YES];
UINavigationController *LogControl = [self.storyboard instantiateViewControllerWithIdentifier:#"LogControl"];
RGLogViewController *logView = (RGLogViewController *)LogControl.topViewController;
[UIView animateWithDuration:0.25 animations:^{
self.presentingViewController.view.alpha = 0;
[self.presentingViewController dismissViewControllerAnimated:NO completion:nil];
[self.presentingViewController.presentingViewController dismissViewControllerAnimated:NO completion:nil];
} completion:^(BOOL finished){
[logView setBoolBackPushed];
}];
}
The above code works in that it takes me back to A by dismissing B and C. However, you can still see the transition because of a bit of "black-screening" from the dismissal of the two ViewControllers. You can see in the completion block [logView setBoolBackPushed];, this sets a static BOOL variable to true so that the beginning of A's viewWillAppear: presents a new UIImagePickerController immediately - the method looks like this:
- (void)viewWillAppear:(BOOL)animated
{
NSLog(#"HERES postBackButtonPushed:%hhd", postBackButtonPushed);
if(postBackButtonPushed == true)
{
self.view.hidden = YES;
self.navigationController.navigationBar.hidden = YES;
self.tabBarController.tabBar.hidden = YES;
UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.delegate = self;
[self presentViewController:imagePicker animated:NO completion:^{}];
}
This is currently how I am getting the following desired effect: Go from A to the camera (B), to C and then back to a new camera which is our new B. This is achieved w/ almost perfect seamless transitions.
Im still having problems with being able to slightly see the transitions. Another problem is the timing. This collaboration of dismissing and presenting ViewControllers takes more time than I would like it to. It's almost instantaneous, but I would like something better. What should I do and am I doing something wrong?
there are some possible cases that cause your problem :
presenting B after A and then presenting C is a bit strange behaviour for both UX/UI and by code. Either push B from A and then present C over B OR dismiss A, present B, dismiss B, present C.
there are completion methods and you are not using them. your code show that you call dismissViewControllerAnimated twice for C and B for ex. you should handle presenting and dismissing order. I think iOS 8.2+ or 8.3+ start to handle them more efficiently. And if there is an error, it will crash after this version.
imagepicker controller cause this error while dismissing VCs in different ways. For ex. showing alert popup before presenting a VC can cause also a crash.
I believe this will work for you.
Do!
[self.presentingViewController dismissViewControllerAnimated:NO completion:^{
[RefrenceOfPreviousViewController dismissViewControllerAnimated:YES completion:^{
}];
}];
Instead of
[self.presentingViewController.presentingViewController dismissViewControllerAnimated:NO completion:nil];
im have two view controller UserListView and UserProfileView!
in UserListView view controller i'm have a button for swtich to UserProfileView and here is code.
UserListView.m - Click Action
- (IBAction)SettingClick:(id)sender
{
UserList *UserProfile = [self.storyboard instantiateViewControllerWithIdentifier:#"UserProfileView"];
[self presentViewController:UserProfile animated:YES completion:nil];
}
And code working fine, when user switch to profile (UserProfileView) have a close button back to UserListView and here is code.
UserProfileView.m - Close click action
- (IBAction)CloseClick:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
UserProfile *UseList = [self.storyboard instantiateViewControllerWithIdentifier:#"UserListView"];
[self presentViewController:UseList animated:YES completion:nil];
}
in this code i will using [self dismissViewControllerAnimated:YES completion:nil]; to close UserProfileView view controller for low ram usage and it work.
But affter i close UserProfileView i want to open this view controller again and it do not work, UserProfileView not showing again??
i using xcode 5 and building an App for ios 7, please help.
Thanks for your time.
If I understand correctly, when you call SettingClick: your app is displaying a UserList. So, when you dismiss a view controller presented on top of it, you should go back to UserList without the need for presenting it again. So you can try with:
- (IBAction)CloseClick:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
This will work unless you had originally presented UserList as well. In this case, UserList will be dismissed with the top controller. In this case, you can delay presenting a second time UserList after dismissing UserProfile, and it should work.
In the latter case, I would suggest you to use a navigation controller instead of simply presenting your controllers like you are doing. As you see, it is not really straightforward and you will get into catches of any kind. Presenting a controller works ok when you present just one controller at a time. On the other hand, if you instantiate a UINavigationController, this will handle the controllers' hierarchy for you.
use this -
- (IBAction)CloseClick:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
This will return to your previous view when u click that button that has been linked with this action
When I call dismissViewControllerAnimated:completion: to dismiss a UIViewController the completion block is never executed when the corresponding view is in the middle of being animated onto the screen (using presentViewController:animated:completion:).
The UIViewController does not even dissappear. It is like dismissViewControllerAnimated:completion: is being ignored.
The following code is a simplified code example because the original is much bigger. The code I have given below simulates a use-case where a network communication error might trigger a view to popup whilst another view is also being popped-up at the same time..
Code example:
NSLog(#"Presenting view");
[self presentViewController:changeLocationViewController animated:YES completion:^{
NSLog(#"View done presenting");
}];
NSLog(#"Dismissing view");
[self dismissViewControllerAnimated:NO completion:^{
NSLog(#"View done dismissing");
}];
Log output is:
2013-08-28 16:14:12.162 [1708:c07] Presenting view
2013-08-28 16:14:12.178 [1708:c07] Dismissing view
2013-08-28 16:14:12.583 [1708:c07] View done presenting
Does anyone know how to dismiss the UIViewController in these circumstances?
Thanks in advance.
The reason this code snippet isn't working is because the completion block in these methods are executed at a later time after the animations have completed. You can see this in your logs: "Dismissing view" happens before "View done presenting". Try this instead:
NSLog(#"Presenting view");
[self presentViewController:changeLocationViewController animated:YES completion:^{
NSLog(#"View done presenting");
NSLog(#"Dismissing view");
[self dismissViewControllerAnimated:NO completion:^{
NSLog(#"View done dismissing");
}];
}];
EDIT:
If you need to make sure the view is dismissed when the network error happens, try setting a boolean instance variable called networkErrorFound.
When you finish the network connection, set this to YES if an error happens. Then use this code:
[self presentViewController:changeLocationViewController animated:YES completion:^{
NSLog(#"View done presenting");
NSLog(#"Dismissing view");
if (self.networkErrorFound) {
[self dismissViewControllerAnimated:NO completion:^{
NSLog(#"View done dismissing");
}];
}
}];
That way, it'll wait until it's done presenting to dismiss. You would also need to handle the case that the error happens after the animation is done (for instance, a slow connection that eventually fails), but that's outside the scope of this question.
Why dont you dismiss it when its done loading?
[self presentViewController:changeLocationViewController animated:YES completion:^{
NSLog(#"View done presenting");
NSLog(#"Dismissing view");
[self dismissViewControllerAnimated:NO completion:^{
NSLog(#"View done dismissing");
}];
}];
OK. It seems like you want to present a VC, but if there is no network found, close the VC. The only reason that I can think of needing to do it this way is if the network only gets checked in the new VC that you are presenting (and want to dismiss if it fails to connect).
And you would be able to achieve that by implementing the code shown in the answer given by #aopsfan.
But think about that UI flow. You are telling a starving man (the user) he can have a sandwich (the next VC that he wants to see)... But WAIT! (dismiss the wanted VC) No, you can't have the sandwich (no network)! Fooled you!.
The way to do it to keep the UI flow nice and not aggravating, would be to check for network connection before presenting the VC. Probably check for network in the IBAction (?) that you use to present the new VC. That way, you can check before presenting. Instead of present-cancel
Heck, you could even show an HUD "in progress" View to let the user know what happening!
I am trying to change my view after the device is rotated. I can't do autorotation support in the view that is drawing because I have custom graphics occurring and it is much easier to reinitialize the view.
The problem I am having is that using [pageViewController setViewControllers:] doesn't seem to want to work inside the didRotateToInterfaceOrientation block.
Here's the method:
- (void) didRotateToInterfaceOrientation:(UIInterfaceOrientation)orientation {
if (UIInterfaceOrientationIsPortrait(orientation)) {
NSLog(#"%#", pageViewController.viewControllers);
isLandscape = YES;
ContentViewController *vc = [self viewControllerAtIndex:currentPage];
NSLog(#"%#", vc);
[pageViewController setViewControllers:[NSArray arrayWithObject:vc] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
NSLog(#"%#", pageViewController.viewControllers);
}
}
which produces this output:
<ContentViewController: 0x74aa8f0>
<ContentViewController: 0xfc0cb00>
<ContentViewController: 0x74aa8f0>
Is there some reason I can't change the view controllers inside this method? If so, where should I be doing this change?
EDIT:
I have been able to bypass the issue using dispatch_async(), but I'd still like to know why this was a problem in the first place.
I recently encountered a hair-pulling situation in my iOS app, where I was trying to successively dismiss one presented UIViewController from my window's rootViewController, using:
[rootViewController dismissViewControllerAnimated:YES completion:NULL]
and present another one shortly thereafter (in another method, incidentally), with:
UIViewController *vc2 = [[[MyViewController2 alloc] initWithNibName:nil bundle:nil] autorelease];
[rootViewController presentViewController:vc2 animated:YES completion:NULL];
Problem was, I could never get the second view controller to show up. Turns out, as near as I can tell, dismissViewControllerAnimated:completion: needs that asynchronous block of "completion" time to pass, before presentViewController:animated:completion: will work properly again. This fact is not directly documented in Apple's docs, from what I can tell.
The solution I came up with was to wrap the dismissal with a method that specifies the selector you would want to call afterwards, like so:
- (void)dismissViewController:(UIViewController *)presentingController
postAction:(SEL)postDismissalAction
{
[presentingController dismissViewControllerAnimated:YES
completion:^{
[self performSelectorOnMainThread:postDismissalAction
withObject:nil
waitUntilDone:NO];
}];
}
And then I would call:
[self dismissViewController:self.window.rootViewController
postAction:#selector(methodForNextModalPresentation)];
Anyway, I wanted to post, as I looked around and hadn't seen anyone with this particular problem, so I thought it might be useful for people to understand. And also, I wanted to verify that I'm not hacking a solution that has a better design pattern for resolution.
Just for the sake of clarity. are you saying that this code doesn't work?
[myRootViewController dismissViewControllerAnimated:YES completion:^{
[myRootViewController pushViewController:newController animated:YES];
}];