I am building a navigation drawer like gmail. and I am using SWRevealViewController pod to handle this
from storyboard I can do it easily, If I want to move from side menu to a particular section, I can make it by using segue, control and drag from side menu to the destination view controller reveal view controller push controller
but now, I need to do it programatically, how to achieve this programatically?
If you have segue with identifier then,
self.performSegue(withIdentifier: "your segue identifier", sender: self)
Add following in your action either in didSelect or under IBAction
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let sw = storyboard.instantiateViewController(withIdentifier: "sw") as! SWRevealViewController
self.view.window?.rootViewController = sw
let destinationController = self.storyboard?.instantiateViewController(withIdentifier: "youIdentifier")
let navigationController = UINavigationController(rootViewController: destinationController!)
sw.setFront(navigationController, animated: true)
Related
I am changing a menu VC over from StoryBoard to programatic operation.
When I want to load another VC I was using the Action Segue "Show".
The temporary code I am using works but pops the VC over the top. Can you action the "Show" equivalent programatically, and if so how?
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "SunriseSunsetResultsViewController")
self.present(newViewController, animated: true, completion: nil)
To do what a Show segue used to do, replace present with show:
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "SunriseSunsetResultsViewController")
self.show(newViewController, sender: self)
Note, however, that this will do the same thing as present unless self.navigationController is not nil. In other words, you cannot do the sideways navigation you were doing before unless a navigation controller is in charge of the interface.
Instead of show you could say pushViewController etc., but the same caveat applies. Only a navigation controller can do what you were doing previously.
I was able to create an iOS application in which you could switch between views with buttons. These views had a navigation controller so you could go back.
However I want to adapt this slightly.
Instead I want to have a single view start with no navigation controller.
Then when a cell in my table view is clicked, I want to navigate to the next view which has a navigation controller.
I can make this happen using segue in the interface builder but I don't want to manage it through the builder. This is because I only want to go to the next view when I have done some checking on the cell clicked in the table view.
So basically I need to programmatically change views.
Here is what I have done so far:
I have an ordinary View Controller.
I then have next to it a Navigation Controller which is linked to a new View Controller after that.
I gave this View a storyboard ID of presetController.
I then have the below code in my table cell onclick function:
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "presetController") as! PresetNavigationController
self.present(nextViewController, animated:true, completion:nil)
Now this code takes me to the next view, but no navigation controller is loaded.
How can I make the navigation controller link to this view? It already is embedded to it via the editor tab link.
Photo to explain story board:
StoryBoard
You need to swap View Controller with Navigation Controller and then do this code:
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "presetController") as! PresetNavigationController
self.present(nextViewController, animated:true, completion:nil)
this will make your navigationbar back button appear
If your link Navigation controller from New controller and Your are opening presntview controller than you will be make as this
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "presetController") as! PresetNavigationController
let navController = UINavigationController(rootViewController: nextViewController)
self.present(navController, animated:true, completion: nil)
Using the suggestions from ppinho I put the navigation controller as the entry point.
As I did not want the navigation controller on the first view I hid it with the below code:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Hide the navigation bar on the this view controller
self.navigationController?.setNavigationBarHidden(true, animated: animated)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// Show the navigation bar on other view controllers
self.navigationController?.setNavigationBarHidden(false, animated: animated)
}
I then added the new view to the navigation like so:
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "presetController") as! PresetViewController
self.navigationController?.pushViewController(nextViewController, animated: true)
Here it is
let nextScreen = storyboard?.instantiateViewController(withIdentifier: "nextScreenIdentifier") as! NextScreenControllerName
self.navigationController?.pushViewController(nextScreen, animated: true)
write this inside any function of the viewcontroller.
I am using AKSwiftSlideMenu as basis for an app.
I have a view controller (HomeVC) that is connected to a navigation controller.
If the storyboard entry point is pointing to HomeVC then I get the menu of course, but my app needs to start without the menu and only after certain screens and tasks that are done i want to navigate to HomeVC and allow user access to the menu.
I noticed that if a i place a button on a starting view controller that does not have a connection to the navigation controller, and connect that button directly to the Navigation controller by drag+cntrl then pressing the button will get me from a view controller without a menu to HomeVC and show the menu.
How do i do that programmatically
Thanks in advance for any help
edit: I have tried the following code
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "Home") as! UINavigationController
self.present(newViewController, animated: true, completion: nil)
which did not work resulting with the following err
Could not cast value of type 'myApp.HomeVC' (0x10a159280) to 'UINavigationController' (0x10cc2d3c8).
let newViewController = storyBoard.instantiateViewController(withIdentifier: "Home") as! UINavigationController
s
"Home" is not a UINavigationController. Remove as! UINavigationController from this line and the error will go away.
thank you Daniel for telling me to instantiate navigation controller. Ended up giving navigation controller a storyboard id named ccontroller and the following code does what I needed
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "ccontroller") as! UINavigationController
self.present(vc, animated: true, completion: nil)
I am attempting to segue to another storyboard programmatically, but every time I've tried the view loads with a black screen with no content.
Here's the code I've been working with:
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let startingView = storyboard.instantiateViewControllerWithIdentifier("HomeScreenView")
self.showViewController(startingView, sender: self)
I've created the reference on the Origin's Storyboard of the Destination Storyboard and have also tried to call:
performSegueWithIdentifier("changeover", sender: self)
Any thoughts and suggestions would be greatly appreciated.
I've haven't had issues using Storyboard References with segues. Here are the steps I've followed to get it working:
1) In the storyboard with the source view controller, drag a storyboard reference onto the canvas.
2) Set the reference to point to the correct storyboard name and view controller in the attribute inspector
3) Ctrl+drag from the source's view controller icon (in the top bar of the scene) to the storyboard reference in order to create a segue.
4) Set the segue to "Show" and give it the identifier "Test" in the attribute inspector
5) In your source view controller's code, at the appropriate location where you want it triggered, add the line:
performSegueWithIdentifier("Test", sender: self)
That should be everything needed for the segue to be called programmatically!
"MyStoryBoard" this is the storyboard name where you want to go
"StoryboardId" this is the controller id which i want to show after segue.
let storyboard = UIStoryboard(name: "Service", bundle: nil)
let myVC = storyboard.instantiateViewController(withIdentifier: "ProfileTimelineVC") as! ProfileTimelineVC
self.present(myVC, animated: true, completion: nil)
In AppDelegate append var window: UIWindow?
let storyboard = UIStoryboard(name: "Registration", bundle: nil)
let registrationvc = storyboard.instantiateViewController(withIdentifier:
"RegistrationViewController") as! RegistrationViewController
self.window?.rootViewController?.present(registrationvc, animated: true,
completion: nil)
In storyboard my app is designed as:
Navigation VC -> VC1
What I am trying to do is to segue to another VC but reset the menu-hierarchy so that i dont have the old VC's stacked in the backgound
What I want to do is:
Navigation VC -> VC1 -> VC2 -> NEW VC with Navigation reseted
So when a user logs in and enters the account VC/Page the navigation stack should be resetted when hitting the account VC/Page
Is this possible to do?
If you do not want to add the view controllers to your navigation controller, every View controller you navigate to you can set that as the root view controller of your navigation controller and it won't be added in your stack.
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let yourViewController: ViewController = storyboard.instantiateViewControllerWithIdentifier("respectiveIdentifier") as! ViewController
let navigationController = self.view.window?.rootViewController as! UINavigationController
navigationController.setViewControllers([yourViewController], animated: true)
My solution same as above but one line less
let yourViewControllerObejct = self.storyboard?.instantiateViewControllerWithIdentifier("YourViewControllerId") as? YourViewController
let navigationController = self.view.window?.rootViewController as! UINavigationController
navigationController.setViewControllers([yourViewControllerObejct!], animated: true)