How call a Modal of another Modal - swift

I know there are other topics on this but I can't seem to find out how to implement it.
I have an app that uses only xibs, no storyboards.
I have a modal with a list of items and I need to show another modal with the details of the selected item.
The error found is that the Modal Detail is being opened below the Modal List, requiring a new user click
Below the code to open the Item List
let modalViewController = DependenteListaViewController()
modalViewController.modalPresentationStyle = .overCurrentContext
present(modalViewController, animated: true, completion: nil)
Below the code to open the Item Detail
let modalViewController = DetailViewController ()
modalViewController.modalPresentationStyle = .currentContext
present (modalViewController, animated: false, completion: nil)
What is the correct way to call a modal of another modal?

To present a modal above another modal properly you should:
Get the view controller that presented the first modal
Use that view controller to present the second modal
That way the original presenting view controller will present the second modal above the first.
Swift
firstModalVC.presentingViewController.present(secondModalVC)

Related

How can I make a Back button on a Tabbed View App in Xcode

I have already multiple views that are in the tab bar at the bottom. In one of those view controllers (home view), I made a button connecting to another page (feed view) that isn't in the tab bar.
How can I make a back button from the feed view to the home view?
I've searched and I've only found tutorials for single view apps.
Reminder: the feed view isn't in the tab bar.
Add a Navbar then connect the left button(Back Button) to the view controller and do self.dismiss(animated: true, completion: nil) inside of that button action event
or
Just link a button from the view and do
self.dismiss(animated: true, completion: nil)
The way to go from tab view to a separate view use this code inside of the button action to go to a non TabBar view
let objSecondVc = self.storyboard?.instantiateViewController(withIdentifier: "ViewController") as? ViewController
objSecondVc!.modalPresentationStyle = .fullScreen
present(objSecondVc!, animated: true, completion: nil)
Then in the seperate View Controller to go back to the TabBar View use
self.dismiss(animated: true, completion: nil)
There is a multiple ways you can do this:
1)
To go to the feed view use present() and then if you want to get back use dismiss()
and the put dismiss function inside of an action of a back button you create in the feed view that you may put it the left upper corner of the view for instance.
2)
You can use NavigationController to go to the feed view and back using the navigation back button.
for me I use the first one because it is more flexible and because I use code instead of storyboard it is easier for me.(Thats just my opinion)

iOS Swift: Dismiss View Controller That Was Presented Using "Show Detail"

I have a view controller that is embedded in a navigation controller. I use show detail in storyboard to show it. How would I dismiss this view controller or go back to previous one?
I know back functionality comes automatically when using "Show Detail" but I need to embed this one in a navigation controller, which doesn't allow the back functionality, so I needed to put a "Cancel" bar button, but It's not working. I tried these ways but didn't work:
dismiss(animated: true, completion: nil)
navigationController?.popViewController(animated: true)
self.presentingViewController?.dismiss(animated: true, completion: nil)
You can add an unwind segue from the detail controller to the master controller.
In your master controller, add a method that handles the unwind action:
#IBAction func unwindFromDetail(segue: UIStoryboardSegue) {
}
Then in the storyboard, control drag from detail controller to the "Exit" of master controller, then select the above method.
Give the segue an identifier and perform it!

How to open View from UIPageViewController and return to back to the UIPageViewController

I'm having issues with opening a view from a UIPageViewController and returning back to the same page on the PageViewController. My current setup is a tutorial, you scroll across the pages to view the tutorial. On the second page it requires the user to make a selection of items off a TableView on another page (by clicking a button). How would I then return back to the second page of the UIPageViewController?
You can use something like this: controller.setViewControllers([self.pages[page]], direction: .forward, animated: false, completion: nil)
Direction and animation is up to You.
You can show second vc from page just by using presentViewController:animated:completion: from your page. Set up some delegation to pass data between parent controller and presented controller.
After selection You just dismissViewControllerAnimated:completion: and You are right back where You are previously.

How to get rid of navigation controller and segue to a controller before it?

Say I have a storyboard a such logInVC->navViewController->RootVC->restOfStack
If in the rootVC I want to logOut, thereby segueing to logInVC without logInVC becoming a part of my stack then how would I do this. I essentially need to get rid of the navigation controller and its stack entirely, returning to the apps launch page that comes before the navVC.
Was surprised to have trouble finding an answer to this one in Swift, would think this is quite a common problem.
By dismissing the root view controller of your key window, you dismiss all view controllers and go back to youe login vc assuming it's the initial view controller.
UIApplication.sharedApplication().keyWindow?.rootViewController?.dismissViewControllerAnimated(true, completion: nil)
You cannot push a ViewController without NavigationController. But there are few ways to achieve what you need.
Make your Login ViewController as RootViewController.
The closest alternative if you don't want to use navigation controller are ModalView controllers.
To present the controller:
self.presentViewController(controller, animated: true, completion: nil)
To dismiss the controller:
self.dismissViewControllerAnimated(true, completion: {});

Modal view controller hides tab bar

In my tab based app, on clicking one of the tabs, I want to display a modal view controller with some info.
In my app delegate's didSelectViewController method, I am adding modal view. But it occupies entire screen and hides the tab bar. I don't want to hide the tab-bar, just want to display modal-view which pops up and can be dismissed.
How do I do it?
Please help.
Thanks in advance.
Modal view controllers are always presented full screen on an iPhone. If you don't want to hide the tab bar, then you need to present this view in some other way besides modal.
I was able to "present" a new view controller, over another, UNDER the tab bar, by setting modalPresentationStyle to .currentContext.
let newViewController = UIViewController()
newViewController.view.backgroundColor = UIColor.red
newViewController.modalPresentationStyle = .currentContext
newViewController.modalTransitionStyle = UIModalTransitionStyle.flipHorizontal
present(newViewController, animated: true, completion: nil)
Edit: From more testing, the above can have some buggy behavior if someone changes the tab WHILE the newViewController is presented.
To "fix," I created a "switcher" - a UIViewController that animates between the view controllers that I want to flip UNDER the tab bar:
view.addSubview(nextView)
UIView.transition(from: currentView,
to: nextView,
duration: 0.5,
options: animation,
completion: { (_) in
currentView.removeFromSuperview()
})
In this case, currentView is the view of ViewControllerOne (the currently visible one), and the nextView is view of ViewControllerTwo (the one we want to present).
For example if secondViewController is the second viewController for your second tabbar, you should do like this:
[secondViewController.view addSubview:theViewYouWantToShow];
In iOS , Modal View Controller is always have highest priority in all view controllers available. So you can not use Modal View Controller in your case.
If you just want to show popup on the screen with back ground visibility, then just use UIAlertView. You can add OK or CANCEL button as per your requirement to remove alert view.
Or
If you want to show a full view with tab bar visibility, then add the view in a that tab as a subview. You can give it a feel like a pop up using transform property of a view.
You can present it modally by setting PresentationStyle.This style presents viewController in a square and it does not occupy full screen.
self.modalPresentationStyle = UIModalPresentationFormSheet;
You can also set the transition:
self.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;