I have a navigation controller A on which i push the view controller B. From B i present modally the view controller C. After I dismiss C, I tried back to A. Therefore the Navigation flow is A->B -> (present ModalView) -> C. I tried without success this code in B:
self.navigationController?.popToRootViewControllerAnimated(true)
Any suggestion on how can i achieve this?
This case just happen in iOS7
Thank you
You have to dismiss modal view controller (C) and popToRootViewController in NavigationController. Try the code below in C View Controller:
let presentingVC = self.presentingViewController!
let navigationController = presentingVC is UINavigationController ? presentingVC as? UINavigationController : presentingVC.navigationController
navigationController?.popToRootViewControllerAnimated(false)
self.dismissViewControllerAnimated(true, completion: nil)
In this case user will see just dismissing modal view controller. Pop to Root View Controller in Navigation Controller will be made in backgroud.
Another option is dismissing Modal View Controller and after that pop to the Root View Controller with animation, then user will see everything.
Code for that below:
let presentingVC = self.presentingViewController!
let navigationController = presentingVC is UINavigationController ? presentingVC as? UINavigationController : presentingVC.navigationController
self.dismissViewControllerAnimated(true, completion: { () -> Void in
navigationController?.popToRootViewControllerAnimated(true)
return
})
Related
I am trying to present two view controllers. I'm trying to avoid presenting one after the other as this doesn't give a good user experience.
I'm using storyboards / segues to present the view controllers, each embedded in navigation controllers.
The behaviour should be:
View Controller 1 presents view controller 2 - but when view controller 2 dismisses I'd like view controller 3 to be the one showing to the user. And ideally an ability to also dismiss to view controller 1.
I understand I can accomplish this with child views. But I'd ideally like to learn how it can be done by manipulating the navigation stack.
I don't think you can do what you want with segue, but certainly you can do it with a little code...
This will (on a button tap, for example) perform a standard slide-in navigation controller animation directly from the current ViewController (call it vc1) to ViewController2, but "insert" ViewController3 into the stack. Tapping the Back button will take you from vc2 to vc3 to vc1.
#IBAction func didTap(_ sender: Any) {
guard let vc3 = storyboard?.instantiateViewController(withIdentifier: "vc3"),
let vc2 = storyboard?.instantiateViewController(withIdentifier: "vc2")
else { return }
let vcArray = [self, vc3, vc2]
self.navigationController?.setViewControllers(vcArray, animated: true)
}
If you want to go from vc2 back to vc1 and "skip over" vc3, in vc2 add (on a button tap, for example):
#IBAction func backToStartTap(_ sender: Any) {
self.navigationController?.popToRootViewController(animated: true)
}
As best to my understanding about your question, try out below method:
Present VC 3 from VC 1 and from VC 3 present VC 2 immediately(This can be done by putting the viewController present code in the ViewDidLoad() of VC 3).
So when you dismiss VC 2, VC 3 will be shown and on dismissing VC 3 , you will be redirected to VC 1.
VC means ViewController.
Presenting viewController in the ViewDidLoad() is really a bad idea.
You can present VC3 and add VC1.view and VC2.view as a subview of VC3's view and later you can remove VC1.view and VC2.view as you want and behaviour will be same as you are expecting.
You need to set your the viewcontroller you are going to present .modalPresentationStyle as .currentContext, and you need to set your current viewcontroller .definesPresentationContext as false
How i can present view controller with all stack from appdelegate.
TabBarContoller is the rootviewcontoller.
I have 4 tabs on TabBarController and each of them are nav controllers.
So i need to present viewcontroller like this.
tabBar => navBar => mainViewContoller => aViewController => bViewController;
And i should be able to use to navigate back to the mainviewcontroller or navigate by tabbar.
I have a storyboard .
I tried a lot of solutions which were recommended by similar questions but it didn't help.
You should implement this through the storyboard only.
I tried sample project using appcoda.I run it successfully that.I show you the storyboard design.
It has implemented using multiple view controllers.
TabBarController * tabBar = (TabBarController *)[UIApplication sharedApplication].delegate.window.rootViewController;
if([tabBar isKindOfClass:[UITabBarController class]]){
[tabBar setSelectedIndex:0];
UINavigationController * navBar = (UINavigationController *)tabBar.selectedViewController;
UIViewController * currentVC = navBar.topViewController;
[currentVC performSegueWithIdentifier:#"notificationSegue" sender:nil];
I thought this might be helpful for all you swift'ers. I have a project setup similarly, LoginViewcontroller -> TabBarController (5 tabs) -> NavigationController for each tab -> Other ViewControllers
In storyboard, I gave my TabBarController a storyboard ID
Within AppDelegate(didFinishLaunchingWithOptions) I added
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "TabBarControllerID") as! UITabBarController
viewController.selectedIndex = 3
self.window?.rootViewController = viewController
self.window?.makeKeyAndVisible()
Change selectedIndex value to whatever tab you need
In viewController1, I have a CollectionView and used the following code to create a segue to the selected item page.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if(segue.identifier == "ToEventInfoSegue") {
let destination = segue.destination as! ViewController2
let cell = sender as! CollectionViewCell
destination.eventInfo = cell.anEvent!
destination.MainVC = self
}
}
Since the viewController2 is not currently embedded in a navigation controller, after the segue push, there is no navigation bar.
My question is in order to get the navigation bar, how do I implement the segue when the VC2 is embedded in a navigation controller?
Update
Tab Bar Controller -> Navigation Bar Controller -> VC1
VC1 -> (Currently no navigation bar controller) -> VC2
Update 2
After resetting the segue, I was able to get a navigation bar in StoryBoard. However, it seems that it is covered by the view. The navigation bar is not showing up. The back button does show up. I think the title is somehow on the bottom of the layer. The hierarchy looks like this.
The blue area is where the title of Navigation Bar is. However, as you can see it is covered and does not show when I run the app. The back button does show up
Based on your question what i understand is you need NavigationBar in your DetailViewController so for that you have to assign segue to your CollectionViewCell with DetailViewController like below screen shot.
I guess your storyboard flow like this:
What you need is assign segue from Cell to DetailViewController:
By doing this you can get your NavigationBar in your detail screen.
Hope this will help you to slove your problem.
In Objective C / iOS;
We have a process similar to this (set up in xcode storyboard);
Menu View Controller -> Enter in a code -> Process/Validate -> Present a Failed Code page
The (->) arrows signify a push segue setup in storyboard
When in failed state I want to pop to the Enter in a code view controller.
UIViewController *vc = nil;
NSUInteger index=0;
for (UIViewController *viewController in self.navigationController.viewControllers) {
if ([viewController isKindOfClass:[SomeViewController class]]) {
vc = viewController;
break;
}
index++;
}
if (vc) {
dispatch_async(dispatch_get_main_queue(), ^(void) {
[self.navigationController popToViewController:vc animated:YES];
});
return;
}
This pops me back to the VC I want to go to.
Except now when I press a submit on the Enter code page it does 3 or 4 more "pushes", when it should only be 1.
Do I need to unwind the segue? I tried emptying the navigational view controller stack, and I even tried ridding it of its last active view controller -- both of these return a blank or black window view frame.
Why would popping a view controller in the navigation stack affect the segues in my view controller to the point where whenever I try to do a push segue action it will try to push multiple view controllers onto the stack?
Turns out I had the following issue
Button press causes segue action in storyboard
I did the same segue action in code on the button, hence pushing multiple times
I have now solved this issue
Is there a way to have one button unwind back to a specific view controller? For example suppose I have ViewController A and B. Both modally segue to ViewController C. Now I understand how to segue unwind back to one of the previous view controllers (As was explained here) but how do I go back to the specific view controller that presented VC C?
So to sum it up, what I'm trying to figure out is...
If A segued to C then I want to unwind back to A when the button is selected. If B segued to C then I want to unwind back to B when the button is selected.
You should use the standard way to return from a modal segue. Put this in your 'back' button...
[[self presentingViewController] dismissViewControllerAnimated:YES];
This doesn't use the storyboard or a segue for the return, just code.
You could use the presentingViewController 'title' property to steer the unwind process to the desired originating ViewController. This solution would also allow you to pass data from your ViewController C to ViewControllers A and B.
1) Select the View Controller button (the yellow one) at the top of the storyboard canvas of ViewController A.
2) In the Attributes Inspector (View Controller section) give ViewController A a title.
3) Include the following code in ViewController A:
#IBAction func returned(segue: UIStoryboardSegue) {
// Don't need any code here
}
4) Repeat steps 1, 2 and 3 for ViewController B.
5) Override the prepareForSegue function in ViewController C as follows:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if self.presentingViewController!.title! == "ViewControllerATitle" {
let destination = segue.destinationViewController as! ViewControllerA
destination.someFunction(someArgument)
}
else if self.presentingViewController!.title! == "ViewControllerBTitle" {
let destination = segue.destinationViewController as! ViewControllerB
destination.someFunction(someArgument)
}
}