I have ViewController created using StoryBoards and I set it's class to be MyViewController.
MyViewController is class that inherits from UIViewController and have some UI elements. One of this elements is UIButton. I can programmatically add actions to this button as addTarget.
I want to to change view in NavigationController when button is clicked
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyboard.instantiateViewController(withIdentifier: "newViewComtroller")
navigationController.pushViewController(newViewController, animated: true)
But nothing happens - why?
Check in your storyboard if your view controller has an embeded Navigation Controller. If not, you can click in your view controller, go to "Editor" on XCode's menu, "Embed in", and choose "Navigation Controller". After that, your view controller will be associated with a navigation controller, and you should be able to use the method "pushViewController" properly.
Related
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.
I have created a TabBarController with 4 UIViewControllers. However, when I want to show one of the ViewControllers in the TabBarController with:
func gotoMainAppVC()
{
let mainVC:SearchVC = SearchVC()
self.present(mainVC, animated: true, completion: nil)
}
I only get a plain black screen. What am I doing wrong?
This is not how any of this works.
I am not sure what self is in your context but I expect it is either a tab bar controller or one of the view controllers on the tab bar.
By calling present it means you want to present a view controller on a current one which by default brings a new fullscreen view controller from bottom upwards.
Since you have created a new instance of view controller by calling SearchVC() this is not the view controller that you see in your storyboard. You may create any instances of any of your view controllers but in your case if you designed SearchVC in a storyboard you want to actually use that version of it. To do so you need to set storyboard id inside your storyboard. After you have done that you should initialise it as using:
UIStoryboard(name: <#T##String#>, bundle: nil).instantiateViewController(withIdentifier: <#T##String#>)
as in
let mainVC:SearchVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "TheIDIHaveSet") as! SearchVC
But again this will not give you the view controller that is inside your tab bar but create a new instance. And it will not instert it as one of the tabs but present a new full screen view controller.
Here are my storyBoards
Tabbar Storyboar
other storyboard
here is my code to Move TabbarStoryBoard to Other Storyboard
let storyboard = UIStoryboard(name: "Tabbar", bundle: Bundle.main)
let vc = storyboard.instantiateViewController(withIdentifier: "LoginController") as? LoginController
self.navigationController?.pushViewController(vc!, animated: true)
Now when I move from Tabbar StoryBoard from Other storyboard's LoginController, and signIn with email & password and want to redirect user to Tabbar storyboard is not pushing. It is successfully pushing from Tabbar storyboard to other storyboard's view controller, even it is not pushing in same storyboard to other viewcontroller
It is not giving any error, and also not pushing to Tabbar Storyboard.
What is my issue?
Note : Tabbar Storyboard's TabbarViewcontroller is my initial viewController
Story Board Referencing is the best thing for this... Hope these links will work.
Use Storyboard References While Retaining Icons & Text for Tab Bar Controller
How to add storyboard reference in another storyboard
I have two view controllers, I added navigation bar to second view controller with two bar button items Back and Item as shown below
But when I do a push segue from first view controller, it is replaced by navigation item <Category, which is title of navigation item in my first view controller as shown below
How do I keep my navigation bar intact avoiding the default navigation item <Category, which is being added automatically while maintaining push segue functionality.
I tried to do maually without using stoyboard as follows
#IBAction func plusAction(_ sender: Any) {
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "SVC") as? SecondViewController
self.navigationController?.pushViewController(secondViewController!, animated: true)
}
but it still doesnt work.
You want to display two bar button items Back and Item by added UINavigationBar to second view controller, you are doing it in the wrong way!!!
In your storyboard, drag a UINavigationItem to your second ViewController.
If the UINavigationItem does not display on your storyboard, you must select second view controller, choose Opaque Navigation Bar or Translucent Navigation Bar (not important)
After that, you can drag UIBarButtonItem where you want on your ViewController
Have you tried changing the kind of segue through segue inspector (click the particular segue and check its attributes in the inspector) like this,
But you should know each one of it is not similar, has it's own definition in how it appears - check https://developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/UsingSegues.html
Also, alternatively using code,
if let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "storyboardId") as? TargetViewController {
self.present(viewController, animated: false, completion: nil)
}
I have navigationController embedded VC(Viewcontroller) in storyboard 1 which is connected to storyboard reference of storyboard 2.
Now, I have VC2 which is again NavController Embedded in storyboard 2.
I am performing the following code :
let storyboard = UIStoryboard(name: "Settings", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "setEdit") as? EditProfile
navigationController?.pushViewController(vc!, animated: true)
settings is storyboard 2, setEdit is ID from navigationController of the destination VC.
When I execute this code, It doesnt perform the presentation of new controller. Also, I have a custom segue class that transitions VCs from Right to Left.
when I use :
present(vc!, animated: true, completion: nil)
It just pushes the VC from bottom to top.
Now I am totally out of Ideas.
My query is:
How can I exactly implement custom segue from one storyboard to another, having navigation bar at the top.
When you use UINavigationController the transition is showed as you said from right to left (push), when you present a view controller modally the presentation style and the transition style are different. Now you cannot connect two navigation controllers, so I suggest to connect directly the controller in the settings storyboard without embedding it in another navigation controller. In settings storyboard you have to set your EditProfileVC as initial view controller and check its identifier and then you can push it from your first storyboard.