I have 3 view controllers I load from story board :
controller1 = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
controller2 = storyboard.instantiateViewController(withIdentifier: "ModelController") as! ModelController
controller3 = storyboard.instantiateViewController(withIdentifier: "BuyController") as! BuyViewController
I would like to create, and load these 3 controllers into a bottom navigation bar with 3 buttons.
Programmatically .
How would I do that with just a simple few lines of code ?
I made a sample Project for you to do that
Check at - https://drive.google.com/open?id=1M12Nv4PLNUePSJFHf7w8b08M2_mWcUEC
Some refereeing code
My Three VC Objects
private lazy var ThirdObject: VC3 =
{
// Instantiate View Controller
let viewController = self.storyboard?.instantiateViewController(withIdentifier: "VC3") as! VC3
// Add View Controller as Child View Controller
self.addChildViewController(viewController)
return viewController
}()
private lazy var FirstObject: VC1 =
{
// Instantiate View Controller
let viewController = self.storyboard?.instantiateViewController(withIdentifier: "VC1") as! VC1
// Add View Controller as Child View Controller
self.addChildViewController(viewController)
return viewController
}()
private lazy var SecondObject: VC2 =
{
// Instantiate View Controller
let viewController = self.storyboard?.instantiateViewController(withIdentifier: "VC2") as! VC2
// Add View Controller as Child View Controller
self.addChildViewController(viewController)
return viewController
}()
Storyboard View
Simulator screen shots
ScreenShot 1 - When no object added
ScreenShot 2 - When Clicked on any button
I clicked on second So showed as
Note: I had used Toolbar with three button You can do same by adding buttons in Navigation bar , Showed you Method how to do , Depend on you that want to make use of Navigation controller or ToolBar at bottom of screen
Related
I want to create a navigation hierarchy where I want to go to a SecondViewController from FirstViewController using a NavigationController. The FirstViewController contains a button B that I intend to use to go to a SecondViewController. I am employing the navigation controller concept since I want to return to the FirstViewController later. I have done the following steps to achieve it:
I have embedded a NavigationController into a storyboard X containing both the FirstViewController and SecondViewController.
I have created a segue from the button B present in the FirstView (has FirstViewController associated with it) to the SecondView (has SecondViewController).
I set the navigationViewController nvc as following after I have presented the firstViewController:
nvc = UINavigationController.init(rootViewController: firstViewController)
Later on, when the button B gets pressed, I execute the following code to push the secondViewController onto the navigation hierarchy:
self.nvc?.pushViewController(secondViewController, animated: true)
However, the secondView doesn't present itself even after pushing.
I have named the segue between the button B and secondViewController as kSegue, so I tried to perform the segue as an addition to see if the secondViewController presents itself or not:
self.performSegue(withIdentifier: "kSegue", sender: self)
An exception occurs when both the 4th and 5th steps are performed together. The exception states that I'm pushing a viewController that already exists on the navigation hierarchy, but the second view still doesn't open even if I comment the performSegue code.
May I ask what mistake I am making here?
In the storyboard, make sure there is a rootViewController segue from the navigation controller to the first view controller. Also, make sure the navigation controller is marked as the initial view controller.
In the code, change
self.nvc?.pushViewController...
To
self.navigationController?.pushViewController...
1) Take a navigation controller on your storyboard
2) Set navigation controller as initial view controller
3) set view controller A as a root view controller of your navigation controller
4) in "GoToB" method access View controller B's instance and push it in navigation controller
5) On View controller B's "Go Back" method write code to pop it.
6) Dont forget to set storyboard Id on both A & B view controller
class FirstViewController: UIViewController
{
lazy var secondViewController : SecondViewController? =
{
let secondViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController
return secondViewController
}()
override func viewDidLoad()
{
super.viewDidLoad()
}
#IBAction func goToB(sender : UIButton)
{
guard let secondViewController = self.secondViewController else
{
return
}
self.navigationController?.pushViewController(secondViewController, animated: true)
}
}
class SecondViewController: UIViewController
{
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func goBack(sender : UIButton)
{
self.navigationController?.popViewController(animated: true)
}
}
I have two controller in the screen as the pic shows.
now I wanna present the ThirdViewController in the red area when the tableView is selected, I try to use present() but it shows in the whole screen. how Can I fix that?
Try this
Download 3 files from Folder Presentation
Add this to the VC from which you will open Second VC
lazy var slideInTransitioningDelegate = SlideInPresentationManager()
When going to another VC;
let vc = self.storyboard?.instantiateViewController(withIdentifier: "Ident") as? YourViewController
vc?.delegate = self
let navigationController = UINavigationController(rootViewController: vc!)
slideInTransitioningDelegate.direction = .bottom // or .left, .right
slideInTransitioningDelegate.disableCompactHeight = true
navigationController.transitioningDelegate = slideInTransitioningDelegate
navigationController.modalPresentationStyle = .custom
self.present(navigationController, animated: true, completion: nil)
Tutorial - https://www.raywenderlich.com/915-uipresentationcontroller-tutorial-getting-started
Project - https://koenig-media.raywenderlich.com/uploads/2016/08/Medal_Count_Completed.zip
You can do this by first adding your SecondViewController into you MainViewController as child controller and then add SecondViewController's view as subview in UITableViewCell.
var secondViewController : SecondViewController!
override func viewDidLoad() {
super.viewDidLoad()
secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
self.addChildViewController(setupProfileViewController)
}
Now somewhere in your code when you want to show that SecondViewController view controller like on didSelectRowAt indexPath method of UITableViewCell or any other button action, add following line of code:
let cell = yourTableView.cellForRow(at: selectedIndexPath)
cell.contentView.addSubview(secondViewController.view)
There is no image attached, so I am not sure what exactly you want; but to show a view controller in area of another view controller, you could use container view, this is a useful tutorial about container view
and to load by some action, you could present your container view when your action done.
I want to push my Navigation Controller but I get :
pushing a navigation controller is not supported
I can't instantiate my TableView.
This is my code :
let newViewController = storyboard!.instantiateViewController(withIdentifier: "player") as! UINavigationController
self.navigationController?.pushViewController(newViewController, animated: true)
So there is a way to push the NavigationController ?
Here we can see the first line means that is the stack of view controller to provide drill down, So you have to push minimum one UIViewController object in the stack or make the root of the stack to any view controller for navigation controller.
From apple document.
A navigation controller manages a stack of view controllers to provide
a drill-down interface for hierarchical content. The view hierarchy of
a navigation controller is self contained. It is composed of views
that the navigation controller manages directly and views that are
managed by content view controllers you provide. Each content view
controller manages a distinct view hierarchy, and the navigation
controller coordinates the navigation between these view hierarchies.
Read this
Set your tableVC as root view controller of navigation controller :
let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var tableViewController = mainStoryboard.instantiateViewControllerWithIdentifier("TableViewController") as! HomeViewController
let navigationVC = UINavigationController(rootViewController: tableViewController)
appdelegate.window!.rootViewController = navigationVC
Now in TableViewController ,to hide navigation bar
self.navigationController?.setNavigationBarHidden(true, animated: false)
Then you can push another view controller like you mentioned
let newViewController = storyboard!.instantiateViewController(withIdentifier: "player") as! UINavigationController
self.navigationController?.pushViewController(newViewController, animated: true)
im guessing there needs to be changes in the app delegate? Thanks for the help!
1) Create a sample Master/Detail via xcode
2) Drag a TabBarController on the storyboard
3) Change the TabBarController to the initial view controller.
4) Control Drag from the TabBarController to the SplitViewController
5) Assign a title to the SplitViewController in the storyboard
6) In the app delegate replace the reference to the SplitViewController as follows.
let tabBarViewController = self.window!.rootViewController as! UITabBarController
print(tabBarViewController.viewControllers?.count)
var splitViewController:UISplitViewController? = nil
for viewController in tabBarViewController.viewControllers! {
if viewController.title == "Master" {
splitViewController = viewController as? UISplitViewController
}
}
let navigationController = splitViewController!.viewControllers[splitViewController!.viewControllers.count-1] as! UINavigationController
navigationController.topViewController!.navigationItem.leftBarButtonItem = splitViewController!.displayModeButtonItem()
splitViewController!.delegate = self
I need to assign data source to a view controller segued by press a button in appdelegate.swift. How can I find the view controller by a segue identifier. I know I can find in a navigation controller by
let navigationController = window!.rootViewController as! UINavigationController
let controller = navigationController.viewControllers[0] as! ListViewController
like this. Is there a way to find a view controller when only the segue is connected with the previous view controller?
In other words, how to identify any vc in a project? Is storyboard ID used for this?
let viewController = segue.destinationViewController as! ListViewController