Dismissing view controllers all at once - ios

I need to go back all the way to the view controller that presented the first navigation controller. However I haven't dismissed multiple controllers before at once, and when I've tried doing so, it doesn't work. It just goes to the first navigation controller instead of all the way to the one before it.
Here is my current code:
[(UINavigationController *)self.presentingViewController popViewControllerAnimated:NO];
[self dismissViewControllerAnimated:YES completion:nil];
I have a view controller which modally presents the first navigation controller. The first navigation controller screen is called Main View Controller. It then pushes to Login View Controller. Login View Controller does presentViewController to MenuViewController (UIViewController).
I need to get from MenuViewController all the way back to the view that presented the first navigation controller. Thanks.

Try this
UIViewController *vc = self;
while (vc.presentingViewController != nil) {
vc = vc.presentingViewController;
}
[vc dismissViewControllerAnimated:YES completion:nil];

One option would be to use NSNotifications.
You can add an observer in your first/root/initial UINavigationController subclass e.g.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(logout:)
name:#"LogoutNotification"
object:nil];
Then in your "logout:" method you have direct control over the initial UINavigationController rather than those further up the hierarchy.
You can then send an NSNotification from anywhere in the app in order to trigger the method.
e.g.
[[NSNotificationCenter defaultCenter] postNotificationName:#"LogoutNotification" object:self];

Related

How to dismiss multiple view controllers which have been presented not pushed?

Scenario:
I need to show 3 or more popups one after the other on button click in each popup. I have created a different viewcontroller and xib files for each popup. So for displaying each popup I have used presentViewController instead of pushViewController.
That is, I have used this:
[self presentPopupViewController:searchPopUpView animationType:0];
instead of
[self.navigationController pushViewController:searchPopUpView animated:YES];
For dismissing a popup, the following code has been written:
[self dismissPopupViewControllerWithanimationType:0];
Issue:
The popups are displaying perfectly, but the background gets darker and darker whenever a popup shows up. After all popups have been dismissed I have to finally click on the blank screen to remove those darker parts. How to overcome this issue?
I think you are using MJPopupViewController to show pop-up.
If it is so, Then try this.
Suppose there is a controllerA from which you want to show a pop-up controller popupControllerB.
Then in your controllerA add Notifications Observer
Code to write in controllerA :
// Add Notification Observer when your view initialise.
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(dismissPopup) name:#"DISMISS_POPUP" object:nil];
In viewWillDisappear remove the notifications observer
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
This method will be called when you Post-notification from your popupControllerB
-(void)dismissPopup {
[self dismissPopupViewControllerWithanimationType:MJPopupViewAnimationFade];
}
And In popupControllerB, Where you want to dismiss the Pop-up, write this code.
[[NSNotificationCenter defaultCenter] postNotificationName:#"DISMISS_POPUP" object:nil];
Above line of code will call a method written in your controllerA and dismiss the pop-up properly.
If you want to dismiss presented UIViewControllers you can use this code. I have used this approach to dismiss presentedViewControllers. It will dismiss all your presentedViewControllers on your rootViewController.
UIViewController* presVC = self.window.rootViewController;
while (presVC) {
UIViewController* temp = vc.presentingViewController;
if (!temp.presentedViewController) {
[vc dismissViewControllerAnimated:NO completion:^{}];
break;
}
vc = temp;
}

SWRevealViewController after a login view

I have an app that have a login view and then after the login is successes I want to have a view with a side menu.
I am using SWRevealViewController to make the slide menu.
But the problem as I said is the login view will be the first view not the SWRevealViewController.
I tried to do the following inside prepareForSegue method.
SWRevealViewController *revealViewController;
[revealViewController initWithRearViewController: [self.storyboard instantiateViewControllerWithIdentifier:#"MenuTableViewCell"]frontViewController:[self.storyboard instantiateViewControllerWithIdentifier:#"HomeViewController"]];
this is not working. That's the only code I have inside prepareForSegue method, I deleted the if statement because I only have one segue inside the loginview so I figured I don't need the IF.
Should I delete the SWRevealViewController from the storyboard? or make the segue from the login view to SWRevealViewController
I don't know what to do.
There's no explanation for situation for using SWRevealViewController inside the views not the first view.
I am building this app for iOS for objective c for iPhone.
Please help.
Thanks.
You should do your storyboard hierarchy like this.
Drag a ViewController and change a class to SWRevealViewController.
Drag a ViewController Embedded in NavigationController. Change class name to LoginViewController.
Change your NavigationController's storyboard ID to "LoginNavigationController".
Assign new custom segue named sw_front from SWRevealViewController to NavigationController and change class of segue to SWRevealViewControllerSegueSetController.
Drag a controller having TableView as a subView which is your MenuViewController for Sidebar.
Assign new custom segue named sw_rear from SWRevealViewController to MenuViewController and change class of segue to SWRevealViewControllerSegueSetController.
Now drag a ViewController named HomeViewController embedded in NavigationController and assign a custom segue with identifier "Home" and class with SWRevealViewControllerSeguePushController.
Change storyboard ID of NavigationController to "HomeNavigationController".
This should be the setup in your storyboard.
Now here is the coding part:
In SWRevealViewController.m
- (void)viewDidLoad{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(userSignedInSuccessfully) name:kUserSignedInNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(userSignOutSuccessfully) name:kUserSignedOutNotification object:nil];
// this flag should be maintained in user defaults
if(iSUserSignedIn){
//Show Home page if user is already signed in
[self showHomeScreen];
}
}
#pragma mark - Show Home screen
-(void)showHomeScreen
{
UINavigationController *navigation = [[UIStoryboard storyboardWithName:kStoryboardName bundle:nil] instantiateViewControllerWithIdentifier:#"HomeNavigationController"];
[self setFrontViewController:navigation];
[self setFrontViewPosition:FrontViewPositionLeft];
}
-(void)showLoginScreen{
UINavigationController *navigation = (UINavigationController *)[[UIStoryboard storyboardWithName:kStoryboardName bundle:nil] instantiateViewControllerWithIdentifier:#"LoginNavigationController"];
[self setFrontViewController:navigation];
[self setFrontViewPosition:FrontViewPositionLeft];
}
Now when user signed first save flag iSUserSignedIn in user defaults and post this notification.
//Post notification for successful sign in
[[NSNotificationCenter defaultCenter] postNotificationName:kUserSignedInNotification object:nil];
When user signed out set flag iSUserSignedIn to nil and post this notification.
//Post notification for successful sign out
[[NSNotificationCenter defaultCenter] postNotificationName:kUserSignedOutNotification object:nil];

Dismissing a Popover created by a segue on iOS 9

There are a lot of questions and answers on Stack Overflow about this but they are just valid for iOS 8 and before.
iOS 9 deprecated a lot of things and the answers on SO did not work anymore.
Said that, I am presenting a popover by performing a segue like this
[self performSegueWithIdentifier:#"myPopover" sender:self];
This segue is created between the current viewController and the viewController used by the popover. There is no button involved. The popover is anchored to a view.
The problem is that on prepareForSegue:identifier
[segue destinationViewController]
is a UIViewController and
[[segue destinationViewController] popoverPresentationController]
is the new UIPopoverPresentationController and this object does not offer a dismiss api anymore.
Instead, we are supposed to use
[self dismissViewControllerAnimated:YES completion:nil];
to dismiss the popover but this has no effect for me.
My situation is this: I have a popover with a text field. I want to dismiss the popover if the user hides the keyboard.
So I did this:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
and then
- (void)keyboardWillHide:(NSNotification *)notification {
[self dismissViewControllerAnimated:YES completion:nil];
}
but this has no effect at all.
I have also tried to create an unwind segue inside the popover viewController and call that from the presenting view controller but that makes the app crash.
Just tried it and it all seems to work perfectly.
What is your view controller hierarchy (navigation controllers, etc.)?
Are you properly calling dismissViewControllerAnimated:completion: on the same view controller that presented the popover?
Make sure there isn't a mixup between child view controller and navigation controller.
You can also log the popover view controller's presentingViewController to check.

Back to RootViewController from Modal View Controller

From Home view - my RootViewController - I open up 2 ViewControllers one after another as user progresses in navigation hierarchy like so:
1) SecondViewController is pushed by button connected in my Storyboard
2) ThirdViewController is presented modally
[self performSegueWithIdentifier:#"NextViewController" sender:nil];
So, the picture is: RootViewController -> SecondViewController -> ThirdViewController
Now in my ThirdViewController I want to have a button to go back 2 times to my RootViewController, i.e. go home. But this does not work:
[self.navigationController popToRootViewControllerAnimated:YES];
Only this guy goes back once to SecondViewController
[self.navigationController popViewControllerAnimated:YES];
How can I remove both modal and pushed view controllers at the same time?
I had a similar situation, where I had a number of view controllers pushed onto the navigation controller stack, and then the last view was presented modally. On the modal screen, I have a Cancel button that goes back to the root view controller.
In the modal view controller, I have an action that is triggered when the Cancel button is tapped:
- (IBAction)cancel:(id)sender
{
[self.delegate modalViewControllerDidCancel];
}
In the header of this modal view controller, I declare a protocol:
#protocol ModalViewControllerDelegate
- (void)modalViewControllerDidCancel;
#end
And then the last view controller in the navigation stack (the one that presented the modal view) should implement the ModalViewControllerDelegate protocol:
- (void)modalViewControllerDidCancel
{
[self dismissViewControllerAnimated:NO completion:nil];
[self.navigationController popToRootViewControllerAnimated:YES];
}
This method above is the important part. It gets the presenting view controller to dismiss the modal view, and then it pops back to the root view controller. Note that I pass NO to dismissViewControllerAnimated: and YES to popToRootViewControllerAnimated: to get a smoother animation from modal view to root view.
I had the same requirement but was using custom segues between the view controllers. I came across with the concept of "Unwind Segue" which I think came with iOS6. If you are targeting iOS6 and above these links might help:
What are Unwind segues for and how do you use them?
http://chrisrisner.com/Unwinding-with-iOS-and-Storyboards
Thanks.
Assuming your AppDelegate is called AppDelegate, then you can do the following which will reset the rootviewcontroller for the app window as the view RootViewController
AppDelegate *appDel = (AppDelegate*)[[UIApplication sharedApplication] delegate];
RootViewController *rootView = [[RootViewController alloc] init];
[appDel.window setRootViewController:rootView];

iOS - Call delegate method on main view from popover inner (pushed) view?

I need to call a delegate method on my main view controller ('showDetails:') from a popover view's pushed view (embedded in navigation controller). This is all from a storyboard setup.
The hierarchy is: Main view -> Popover (menu tableview embedded in navigation controller)->Popover secondary View (pushed onto popover navigation controller)
I know how to setup a delegate on the popover using prepareForSegue, but not on an inner view.
How can I call a delegate method on the main view from an inner (pushed) view of a popover?
Here is how I setup the delegate on a popover main view:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"segueSearchResults"]) {
//Dismiss User Popover
[self dismissUserPopover];
SearchResultsViewController *vc = segue.destinationViewController;
vc.searchDelegate = self;
self.searchPopover = [(UIStoryboardPopoverSegue *)segue popoverController];
self.searchPopover.delegate = self;
}
}
Instead Delegate i prefer "NSNotificationCenter" in your case
Add an observer to your ViewController for some action in uiview
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(receiveActionNotification:)
name:#"someActionNotification"
object:nil];
Post Notification from your pushed View in PopOverController
Post Notification and method in your Viewcontroller will be called
[[NSNotificationCenter defaultCenter] postNotificationName:#"someActionNotification" object:self];
At the end Dont forget to remove Observer.
[[NSNotificationCenter defaultCenter] removeObserver:#"someActionNotification"];
When you need to communicate between two view controllers which are far apart in the VC hierarchy, trying to reference one from the other so you can directly call methods on it doesn't work so well -- there's several levels of indirection in between, and it's very fragile if you change your VC hierarchy later.
Look into notifications (NSNotificationCenter) instead; you can have one VC "broadcast" info for another to respond to, regardless of where they are in your app.

Resources