How to navigate from popoverpresentation view controller to another view controller - ios

I have a screen with a navigation controller and it has a button on the nav bar which displays a table (from another view controller) to select things from using pop over presentation, now on clicking any of those items i want to open another view controller a different screen.
BUT if i use navigationController?.pushViewController(tab, animated: true)
the new view controller is displayed within that small pop view itself
and if i use
navigationController?.presentViewController(tab, animated: true)
the navigation bar isn't there on that screen and i cannot go back to the previous screen. How to do it in such a way that i can go back to the screen which first displayed the pop Up list.

If you are using a Storyboard, it's really easy to do. If you're not, then you should. It's very good to use a Storyboard.
Let's call your view controllers these names:
The view controller that can show the popover is called SourceVC
The popover controller is called PopoverVC
The view controller to show when the user selects something from the table view is called NewVC
Add a show segue connecting your SourceVC to your NewVC. Give the segue an identifier.
Add an unwind segue that unwinds from PopoverVC to SourceVC. First, add these methods in your SourceVC:
func unwind(segue: UIStoryboardSegue) {
if let vc = segue.sourceViewController as? PopoverVC {
// get the thing that the user selected and store it somewhere
// perform a segue that shows NewVC
}
}
override func prepareForSegue(segue: UIStoryboardSegue) {
if let vc = segue.destinationViewController as? NewVC {
// pass the thing that the user selected to the NewVC
}
}
Then, select the PopoverVC and control drag it to the "Exit" thingy in SourceVC. And select "unwind:". Give this unwind segue an identifier as well.
When the user selects a row in the table view, just perform the unwind segue and store the thing that the user selected in a class-level variable so that you can pass it to SourceVC.

Related

How to Connect More than One View Controller to Navigation Controller

I have the following storyboards:
"Home" is the default view controller. When you press the button in the top left with three lines, the menu view controller slides out (it's like a side menu). Within the menu there are four table cells that represent menu items, as you can see. When a cell is pressed, I have a corresponding function that is called. I want the view controller on the far right to be presented when a cell is pressed.
Here's the issue: I want the far right view controller to inherit the properties of the Home view controller, such that the navigation title and button are still there. How can I achieve this?
Here's the solution:
Make sure that the navigation and right view controller are segued (Ctrl + Drag in Interface Builder)
Call the code stated in #bebzerk answer:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let transition = storyboard.instantiateViewController(withIdentifier: "RightViewController") as! RightViewController
navigationController?.pushViewController(transition, animated: true)
Now go back to Interface builder, and add a UINavigationItem and a UIBarButtonItem to the view controller. Set the image of the button to the three lines and set the title of the navigation item to the name you want displayed on the top.
In the ViewController Swift file for the right view controller, Ctrl + Drag the bar button item and create an IBAction function. This will be called when the menu button (on the far right view controller is pressed). For me, this class extends from HomeViewController, so in the function, just called the super method. It should look like this:
#IBAction override func menuTapped(_ sender: UIBarButtonItem) {
super.menuTapped(UIBarButtonItem())
}
This should achieve the desired function.
Did you try this way ?
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let transition = storyboard.instantiateViewController(withIdentifier: "RightViewController") as! RightViewController
navigationController?.pushViewController(transition, animated: true)
It should open your far right view controller and makes it inherit your home controller, like with a back button
Test it and tell me if it's good to you.

unable to show controller over the current context with tabbar swift

hello my controller is connected to tabBar and navigationController now I want to show the controller over the current context then move to another controller B which must show the tab bar and back button of the navigation , currently i am doing segue its showing me black background.
Below is my UIStoryBoard layout.
i am moving to next viewController using segue like below
performSegue(withIdentifier: "second", sender: self)
Select the segue and change it's attributes as following
//
set id for the VC and use this instead of a segue after you dismiss the model
let vc = self.storyboard?.instantiateViewController(withIdentifier: "Id")
self.presentingViewController?.navigationController?.pushViewController(vc!, animated: true)

How I could clean UINavigationBar transitions history?

I currently have parental "menu" TableView with UINavigationBar and from each cell there is a segues by reference outlet to 3 similar Views with different information.
In each View there is a buttons to other 2 Views.
With every button's segue opens another View.
The problem:
From every View UINavigationBar's back button returns me to previous View but i tries to make back button to "menu".
Additional Bar Button Item and segue from it makes very close effect but segue animation is not like in UINavigationController.
How I could clean UINavigationBar transitions history in segue to initial View?
You can try pop to root view controller or You can edit navigation controller viewControllers property and remove/add some VC in between.
You can try Unwind Segue mechanism too.
Here are some methods(function) that navigation controller providing for pop operations. They are returning optional UIViewController (intance) from it’s navigation stack, that is popped.
open func popViewController(animated: Bool) -> UIViewController? // Returns the popped controller.
open func popToViewController(_ viewController: UIViewController, animated: Bool) -> [UIViewController]? // Pops view controllers until the one specified is on top. Returns the popped controllers.
open func popToRootViewController(animated: Bool) -> [UIViewController]?
Here is sample code as a solution to your query::
// if you want to back to root of your app
if let rootNavigationController = self.window?.rootViewController as? UINavigationController {
rootNavigationController.popToRootViewControllerAnimated(true)
}
// But if you want to back to root of your current navigation
if let viewcontroller = self.storyboard?.instantiateViewController(withIdentifier: "NewViewController") as? NewViewController { // or instantiate view controller using any other method
viewcontroller.navigationController?.popToRootViewControllerAnimated(true)
}

Show Segue from button opens without navigation controller

I have two view controllers and a Navigation contoller in a storyboard. In the first view controller I have two buttons.
Both buttons segue to the second view which contains a map and opens the map with different info. They are both of the kind show.
The first button when clicked opens the map with the navigation bar at the top (it loads from the side).
The second button loads the map without the navigation bar (it loads from the bottom).
I want both buttons to load the map with the navigation bar.
I am using xcode 7.3 swift and storyboards
Also using prepare for segue
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "lav"){
let DestViewController = segue.destinationViewController as! MapView
DestViewController.type = typeLav
}
if(segue.identifier == "cam"){
let DestViewController = segue.destinationViewController as! MapView
DestViewController.type = typeCam
}
}
Set the segue action
Present Modally
Show Up from bottom, and without navigation controller, so there is no navbar too.
Push
Push the view controller to current navigation stack, so the navigation bar is shown
btw, method prepareForSegue was used to setup data transfered between view controllers;

Tab missing on VC linked to Tab Bar Controller

I have a set of view controllers linked to a tab controller.
When a image is selected on one of these tabbed views a segue is executed and a detail viewocntroller not linked to the tab controlled is opened.
Pronblem is when I navigate back to the tabbed view controller via segue from the detail view, the tabs are no longer visible.
Before segue executed on tabbed vc:
Same vc with no tab after segue from vc not linked to tab controller
Question is how to ensure tab will be visible on vc when called via the non tabbed vc?
Just to add the tabbed view connected to tab controller is a collection view, the detail view segue is excited when user sects an image:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
println(segue.identifier)
println(sender)
println("SEGUE SELECTED: \(segue.identifier)");
if(segue.identifier == "segueToDetailScrollView"){
// pass the cell
let cell = sender as CollectionViewCell;
let indexPath = collectionView?.indexPathForCell(cell);
let vc = segue.destinationViewController as ScrollView;
var image = arrayOfIUmages[indexPath!.row];
var imageTitle = titles[indexPath!.row];
println("Image to segue name : \(image) and the title : \(imageTitle)");
println("The vew controller \(vc)");
vc.currImage = UIImage(named: arrayOfIUmages[indexPath!.row]);
vc.textHeading = self.titles[indexPath!.row];
}
}
You will need to create a segue to the tabcontroller and let tab controllers set the inner view. Onload you can check for a shared default or something similar to find which tab should be shown.
Need to use unwind method in target view controller to allow navigation back to the same instance navigated from, good example here http://www.raywenderlich.com/81880/storyboards-tutorial-swift-part-2

Resources