How to dismiss viewcontroller in ios? - ios

I created a library, and if the main app call my library its showing it, and download some data from server. But if the server has some error I would like to kill the library view, but it's not working
I have a delegate in the host app:
-(void)libraryResult:(NSString*)result{
NSLog(#"result: %#", result);
}
And I download data from server in the viewWillAppear method, and the download has a delegate method like this:
-(void)networkManagerError:(NSString *)error{
[hud hide:YES];
[self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
[self.delegate libraryResult:error];
}
I see in the log, that the app return to the main app, but the view don't change.
How to solve this? Whats wrong with my code?

Change this line
[self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
To
[self dismissViewControllerAnimated:YES completion:nil];

try these..
[self dismissViewControllerAnimated:YES completion:nil];

If you have VC1 which presents VC2, then inside VC2 at the corresponding event (say a Close button tap, or error from server etc.), you should call:
[self.presentingViewController dismissViewControllerAnimated:YES completion:^{
}]
If you set up things in such a way that VC1 is notified about those events that happen inside VC2, you can use:
[self dismissViewControllerAnimated:YES completion:nil];
However, the 1st method is more preferred, as it's a better design practice, and contributes to more loose coupling between VC1 and VC2.

Try to fire a notification from your "library result" method if error occurred and add an observer to your current controller view.
[[NSNotificationCenter defaultCenter] postNotificationName:Remove_CurrentView object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(remove) name:Remove_CurrentView object:nil];
-(void)remove{
[self dismissViewControllerAnimated:YES completion:nil];
}

Related

Attempt to dismiss from view controller (UIModalViewController) while a presentation or dismiss is in progress

I have just started working on ios.
I created a Modalviewcontroller (VC1) and presented another modalViewcontroller (VC2).
There is a button (dismiss) on VC2 which will have to dismiss both viewcontrollers.
The way i know is call :-
[self dismissViewControllerAnimated:YES completion:nil];
in VC2
then call the same in VC1
So i created a delegate which tells me if dismiss is clicked in VC2.
so when dismiss is clicked:-
i call
[self dismissViewControllerAnimated:YES completion:nil];
in VC2
then that delegate method takes me to VC1
where I again call
[self dismissViewControllerAnimated:YES completion:nil];
This method was perfectly working till i was using the app in ios9
when i shifted to ios7 i started getting the warning and VC1 was not getting dismissed.
Please let me know why is this happening.
So the part which works for me as told in comments.
[self.presentedViewController dismissViewControllerAnimated:YES completion:^{
[self dismissViewControllerAnimated:YES completion:nil];
}];
So, error tells you exactly what happened. You trying to dismiss VC1, while your VC2 dismissing. By putting dismissViewControllerAnimated into delegate method does not guarantee that VC1 will be dismissed before, instead of that you should call your [self dismissViewControllerAnimated:YES completion:nil]; in completion block after first dismiss, so your code will look like that:
[self dismissViewControllerAnimated:YES
completion:^{
[self dismissViewControllerAnimated:YES completion:nil];
}];

Simultaneously dismissing a view controller and then presenting one

So I have a VC that i call on when a button is clicked (It's a MFMailComposeViewController) and when the message is sent, It dismisses the ViewController and i want it to present a different one once it's done sending. But instead of doing this it crashes after sending the email every time. i know I'm doing something wrong but i'm not sure what.
Here's my code that dismisses and presents the new one.
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
[self dismissViewControllerAnimated:true completion:nil];
[self presentViewController:_emailConfirmationPage animated:YES completion:nil];}
I was thinking that the problem was that I used self but I'm not sure what I use in place of that.
It is crashing because you are asking for both dismiss and present animations to begin simultaneously. So you can fix this in 2 ways:
Wait for the first animation to complete before starting the next one. This will animate the dismissal of the current VC and animate the presentation of the new one. Of course the user would have to wait for both animations to complete before they can continue interacting with the app. To do this, present _emailConfirmationPage in the completion block of dismissing the current VC like this:
[self dismissViewControllerAnimated:YES completion:^{
[self presentViewController:_emailConfirmationPage animated:YES completion:nil];
}];
Animate either dismiss OR present, but not both. This might be better because the user would have to wait only for 1 animation and that 1 animation will most probably be sufficient to ensure a fluid user experience.
[self dismissViewControllerAnimated:NO completion:nil];
[self presentViewController:_emailConfirmationPage animated:YES completion:nil];
Try put your [self presentViewController:_emailConfirmationPage animated:YES completion:nil];} into completion: block in the [self dismissViewControllerAnimated:true completion:nil]; , it will execute present vc after it has complete dismiss the other vc
Like this:
[self dismissViewControllerAnimated:YES completion:^{
[self presentViewController:_emailConfirmationPage animated:YES completion:nil];
}];
Your code did not work because its still animating dismissViewController and there can't be 2 animation at the same time

iOS popToRootViewControllerAnimated what method will be called after?

I have a strange problem in my app. When I'm pressed backButton from UINavigationController I trying to handle it with -(void) viewWillDisappear:(BOOL)animated in a next way:
-(void) viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated] ;
[self dismissViewControllerAnimated:YES completion:^{
[self.navigationController popToRootViewControllerAnimated:YES];
}];
}
After it I want do some code in -(void)viewDidAppear:(BOOL)animated of my rootTableViewController. But it's seems like that method wasn't called, but my controller
is changed to root(visual at least ). Have you any idea what I do wrong & how to resolve it?

dismissViewControllerAnimated calling method in another view

I am new to iOS Programming and now i have a problem.
There is a login screen that use [self presentViewController:loginview animated:YES completion:nil]; to show it out in MasterViewController.
And now i want to call a method (reload data) after [self dismissViewControllerAnimated:YES completion:nil]
this is my code:
[self dismissViewControllerAnimated:YES completion:^ {
[self getPostData]; // Reload Data
[self.tableView reloadData]; // Reload Data
}];
but it is not working.
(the reload method is in the MasterViewController)
Anyone can help me?
You can use NSNotificationCenter for your problem.
Define a NSNotification in your MasterViewController viewDidLoad like below
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(closeModal:) name:#"CloseModal" object:nil];
And then define the method as below
-(void)closeModal:(NSNotification *)notification{
UIViewController *controller=(UIViewController *)notification.object;
[controller dismissViewControllerAnimated:YES completion:^ {
[self getPostData]; // Reload Data
[self.tableView reloadData]; // Reload Data
}];
}
And at last from your other controller from where you are actually trying to dismiss your controller use code below
[[NSNotificationCenter defaultCenter] postNotificationName:#"CloseModal" object:self];
what you should do is basically call method on presenting view controller as below
[(MasterViewController*)self.presentingViewController reloadData];
You can call the presentingViewController's methods which updates the Presenting view.
for example: I have a modal view which can be launched from multiple Views. And all the presenting views have the view reloading code within view will appear.
Add the below code before dissmissing the modal view.
[self.presentingViewController viewWillAppear:YES];
[self dismissViewControllerAnimated:YES completion:Nil];

Can't dismiss FBfriendPickerViewController from delegate method

I am using the facebook FBFriendPickerViewController and want the user to only be able to select one friend and then have it dismiss the view controller. In the FBFriendPickerDelegate, under the selectionDidChange method I am trying to capture what friend they selected and then dismiss the view controller. I can't get it to dismiss, I feel like I have done this type of thing many times before so I feel kind of dumb asking this, but I feel like I have exhausted every variation of this and nothing works.
-(void)friendPickerViewControllerSelectionDidChange:(FBFriendPickerViewController *)friendPicker{
self.selectedFriends = friendPicker.selection;
NSLog(#"%#", self.selectedFriends);
[friendPicker dismissViewControllerAnimated:YES completion:nil];
}
I have also tried
[[friendPicker parentViewController] dismissViewControllerAnimated:YES completion:nil];
[self.friendPickerController dismissViewControllerAnimated:YES completion:nil];
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
[[friendPicker navigationController] dismissViewControllerAnimated:YES completion:nil];
[self dismissViewControllerAnimated:YES completion:nil];
all to no avail.
Since it's a navigation Viewcontroller that you likely pushed, you would want to pop it:
[self.navigationController popViewControllerAnimated:YES];
If you present the friendpicker controller modally you can dismiss it by using the facebookViewControllerCancelWasPressed and the facebookViewControllerDoneWasPressed methods of the FBViewControllerDelegate protocol. If you conform to FBFriendPickerDelegate you automatically conform to the first one.

Resources