Embed a Uiviewcontroller in a Navigationcontroller Using Segues

Embed a UIViewController in a NavigationController using segues

In your Storyboard, you can embed a ViewController in a Navigation Controller by selecting the View Controller and then picking from the menu at the top Editor->Embed In->Navigation Controller. From another view controller, you control drag to this Navigation controller to set up the modal segue. You can also control drag to the original View Controller to set up segues to it without the Navigation Controller.

Storyboard showing a view controller both embedded in a navigation controller and not

Embedding an existing UIViewController into a new UINavigationController in Interface Builder

You can do it following these steps:

Select your viewcontroller from editor select embed in then select navigation controller from option

And your view controller will get new navigation controller with relationship segue.

Hope this will help :)

How to embed a UINavigationController in prepareForSegue?

The segue you're intersecting is to your VC2, not a navigation controller. You're going to need to update the "AddSegue" segue to point to a navigation controller, then link your VC2 as the root view controller of the navigation controller.

If you post your storyboard, I can give you more details on how to do this.

EDIT: Here's how you should set up your storyboard. It might get a bit weird, since VC2 has two parent navigation controllers, but it should be fine.

Sample Image

How to embed a view in a navigation controller programmatically?

You must embend your UIViewController inside the Navigation Controllers and initialize your tab menu with your Navigation Controllers.

Also for each tab you will have different Navigation Controller

Your code should look like that.

import UIKit

class TabBarController: UITabBarController {

override func viewDidLoad() {
super.viewDidLoad()
let mController = MViewController()
mpController.tabBarItem = UITabBarItem(title: "view1", image: UIImage(named: "viewoneimage"), tag: 0)
let inputController = InputViewController()
inputController.tabBarItem = UITabBarItem(title: "Input", image: UIImage(named: "plus"), tag: 1)
let aController = ATableViewController()
aController.tabBarItem = UITabBarItem(title: "custom", image: UIImage(named: "person.fill"), tag: 2)
let navMController = UINavigationController(rootViewController: mpController)
let navInputController = UINavigationController(rootViewController: inputController)
let navaController = UINavigationController(rootViewController: aController)
viewControllers = [navMController, navInputController, navaController]
}
}

Segue to a view controller embedded with a tab and navigation controller

You have to change the tabBarController selected item index programmatically like this when the button is tapped,

self.tabBarController?.selectedIndex = 1 // 0 or 1 as you have two tab items

And you have to remove the segue for that ViewController 2 as it is the first ViewController in that tab so once you set the selectedIndex of tabBarController you will get that ViewController

SWIFT 3: Segue with navigation controller

For the first Problem, When using embedded in navigation controller you have don't have to create action function when the button is tapped. Navigation controller does it for you. So things you need to do :

  1. Disconnect the function btnTapped from the button using storyBoard.
  2. Delete or comment the function btnTapped(You don't need it).

Segue from one view controller embedded within UINavigationController to another UINavigationController

Push segue occurs only within one navigation controller. It's how it implements 'show' type segue. Making 'show' segue from navigation controller to another navigation controller is not what Xcode drawing tool considered as 'pushing'. It interprets it as popover by default.

Passing Data between view Controllers Using a segue from a view embedded in a navigation controller to a tabbarcontroller

This is my view controller where you can check that I am sending 5 to tabbar first viewcontroller:

   class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.performSegue(withIdentifier: "segueIdentifier", sender: self)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let barViewControllers = segue.destination as! UITabBarController
let destinationNv = barViewControllers.viewControllers?[0] as! UINavigationController
let destinationViewController = destinationNv.viewControllers[0] as! FirstViewController
destinationViewController.currentBalance = 5
}
}

Now You can check my firstview controller where you can check that what value we are getting.

class FirstViewController: UIViewController {

var currentBalance = 0
override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
print(currentBalance)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}

Now, You can check my console and storyboard:
Sample Image

Sample Image



Related Topics



Leave a reply



Submit