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
Related
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.
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
})
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)
}
}
I setup a storyboard based on the Master-Detail Application, embed the detail view in a navigation controller, and add a new table view controller object which I will use as a second detail view controller.
I then push the new detail view controller with the following code (instead of a segue because I am pushing both a root view and a detail view controller at the same time. Only the detail view code is shown).
// Push the detailView view controller:
NewClass *newViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"test"];
newViewController.navigationItem.hidesBackButton = YES;
self.splitViewController.delegate = newViewController;
[self.detailViewController pushViewController:newViewController animated:YES];
This works perfectly, EXCEPT that the splitView delegate methods are never called before or after the push. If I do this while in portrait mode, after it pushes the detailViewController, the button to drop down the masterView popover does not show up UNTIL I rotate to landscape mode and then back to portrait mode.
How can I cause the willHideViewController/willShowViewController split view controller delegate methods to be called or manually cause them to be called?
So from what I found, it doesn't call the method because the orientation hasn't changed.
What you have to do is to pass the button from the presenting view controller since it's already tied to the popover like this:
if(self.navigationItem.leftBarButtonItem != nil) {
newViewController.navigationItem.leftBarButtonItem = self.navigationItem.leftBarButtonItem;
}
// Push the newViewController
As you know, a UISplitViewController has one root controller and one detail view controller only, but I want to use another detail view controller.
When I select the list items from the root controller (popover controller), the selection should fire different detail views -- i.e., row1 fires detail view1, row2 fires detail view2 and a button item fires detail view3, etc.
How can I achieve this?
That project from Apple is from 2012 and doesn't use storyboards. If you are looking for a non-storyboarded solution, it will work fine but in Xcode 6 you should be taking advantage of the new Show Detail segue in storyboards.
Here's a quick example project that shows how to use multiple detail view controllers on the same split view by using the Show Detail segue from the Master View Controller.
There's a project from Apple that covers exactly what you need. MultipleDetailViews
This sample shows how you can use UISplitViewController to manage
multiple detail views.
The application uses a split view controller with a table view
controller as the root view controller. When you make a selection in
the table view, a new view controller is created and set as the split
view controller's second view controller.
The root view controller defines a protocol
(SubstitutableDetailViewController) that detail view controllers must
adopt. The protocol specifies methods to hide and show the bar button
item controlling the popover.
In Swift
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let imageGalleryVC = storyBoard.instantiateViewController(withIdentifier: "ImageGallerySID") as! ImageGalleryViewController
splitViewController?.showDetailViewController(imageGalleryVC, sender: nil)
}
I know this is a late post as this was asked 6 years ago and active last year.
But there is a way to have multiple detail views for a split view controller.
By embedding each detail controller into its own navigation controller and linking from the master view to each using the 'show detail' segue, you are able to achieve this result of switching between views by using an identifier associated and then from the master view function 'didSelectRowAt' selecting a row is where you can select which detail view you wish to see.
if indexPath.row == 0 {
performSegue(withIdentifier: "secondView", sender: self)
}
if indexPath.row == 1 {
performSegue(withIdentifier: "thirdView", sender: self)
}