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.
Related
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 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.
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.
I am trying to display a fix navigation bar for my UiTableViewController, I have a first ViewController and when I click on it, this will open my UITableViewController Here is the code of the click :
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("MyTableViewController") as! MyTableViewController
vc.myObject = object // I pass some data
presentViewController(vc, animated: true, completion: nil)
the UItableView is correctly display but not navigation bar appear, if I add one, the navigation bar scroll with the table view and I don't want this behavior.
I tried this without success :
Go to the Editor menu, and click on the Embed In submenu, and choose
Navigation Controller
And tried to change some settings here :
Actually, in your case you want to show navigation and for navigation you have to push your view controller to a UINavigationController thats why the solution is :
let vc = storyboard.instantiateViewControllerWithIdentifier("MyTableViewController") as! MyTableViewController
vc.myObject = object // I pass some data
self.navigationController?.pushViewController(vc, animated: true)
presentViewController offers a mechanism to display a modal view controller; i.e., a view controller that will take full control of your UI by being superimposed on top of a parent controller & establish a parent child relation b/w presenting & presented view controllers.
where as
pushViewController offers a much more flexible navigation process where you can push & pop a new controller to UINavigationController, so to go back to the previous one, in a ordered way. Imagine that controllers in a navigation controller will just build a sequence from left to right like building a stack of view controllers stacking upon each other.
Do it this way:
let vc = storyboard.instantiateViewControllerWithIdentifier("MyTableViewController") as! MyTableViewController
vc.myObject = object // I pass some data
self.navigationController?.pushViewController(vc, animated: true)
This question already has an answer here:
self.tabBarController dismissViewControllerAnimated doesn't work
(1 answer)
Closed 6 years ago.
I'm having a hard time presenting a tab bar controller that is not the root view controller.
I have the current setup:
I want to press a button in my main view controller and be able to present the tab bar controller with the option to go back to the main view controller.
I tried creating a class of type UITabBarViewController, associating it to my Tab Bar Controller and just presenting it but it does not work.
I would like to present the tab bar controller with the favorites tab selected.
What I tried:
let vc = TabBar()
self.presentViewController(vc, animated: true, completion: nil)
You can switch tabs by setting a selected index property of UITabBarController. Like this:
tabBarController.selectedIndex = 1
You don't need to create new view controllers or perform segues if all you want is to switch between the two tabs.
You can do it in tow manner :
using segue :
Drag from button to the tabBarViewController And choose a type (Modal, Push(if your mainViewController is NavBarVC) ...)
from code :
click on your tabBarViewController and go to the attributes inspector and give your VC a storyboard id
and from code :
let mainST = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let VC = mainST.instantiateViewControllerWithIdentifier("idTabBar")
presentViewController(VC, animated: true, completion: nil)
Edit dismiss tabBar
if the tabBar is presented modally,
to dismiss it you have tow choices :
1) using a delegate :
protocol ExitMe {
func exitMe()
}
In the view controller presenter of the tabBar
extension PresenterOfTabBar: ExitMe{
func exitMe(){
dismissViewControllerAnimated(false, completion: nil)
}
}
and in the tabBarViewController define an exitDelegate variable var exitDelegate: ExitMe! and set it's value from the presenter. When the user click a button to exit tabBar you just call exitDelegate.exitMe()
using an unwindFuction when presenting modally using a segue:
in the presenter you define a function like this
#IBAction func unwindFromTabBar(sender: UIStoryboardSegue){
// do what you want here
}
and in the InterfaceBuilder drag from the the button that should exit the tabbar to the exit in the view controller dock then choose the func unwindFromTabBar.
Others solutions may exist (using notification, get the prsenter View controllers ....) you should pick the suitable one...