hello my controller is connected to tabBar and navigationController now I want to show the controller over the current context then move to another controller B which must show the tab bar and back button of the navigation , currently i am doing segue its showing me black background.
Below is my UIStoryBoard layout.
i am moving to next viewController using segue like below
performSegue(withIdentifier: "second", sender: self)
Select the segue and change it's attributes as following
//
set id for the VC and use this instead of a segue after you dismiss the model
let vc = self.storyboard?.instantiateViewController(withIdentifier: "Id")
self.presentingViewController?.navigationController?.pushViewController(vc!, animated: true)
Related
In my iOS app, in a viewController I try to open a specific view in Tab Bar Controller.
I use a performSegue.
First I try to navigate straight to the specific view , but in this case the tab bar disappear
So I try to navigate to the tabViewController, and this lead me to the defult view (the first)
any idea how to navigate to a specific view in TabBarController ?
the performSegue I use:
self.performSegue(withIdentifier: "goToMain", sender: self)
//"goToMain" is my indentipier to the storyboard Segue
I use swift 4
with this code, you don't need segues, you can use when you push some button
let VC1 = self.storyboard!.instantiateViewController(withIdentifier: "tabBarController") as! tabBarLoginViewController
VC1.selectedIndex = 2 //this line says that the view that appears will be third of you tab bar controller
self.navigationController!.pushViewController(VC1, animated: true)
if you want to use segue use this, the segue needs to point of tab bar controller, not a view of tab bar controller
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "goToMain") {
let vc = segue.destination as! TabBarController
vc.selectedIndex = 2
}
}
I have a view controller connected to a popover controller, which directs to a new view controller. I want to add a back button on the navigation item to the last view controller so that when I click back it will return to the first view controller
I tried creating segue and click action in the popover controller and add back button in the segue function/click action function
neither of them works
This is what I do in the Popover. I already present the popover with UIPopoverPresentationControllerDelegate and this is what PopoverVC looks like. In this way, I add IBAction to enable the click action of the label in popover. I write the backbutton in the IBAction function and create the navigationcontroller with rootview, and the back button doesn't appear.
class PopoverViewController: UIViewController {
//TODO: add BACK button
#IBAction func createNewChat(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle:nil)
let contactVC = storyboard.instantiateViewController(withIdentifier: "contacts") as? NewContactsViewController
let nc2 = UINavigationController()
let back = UIBarButtonItem()
back.title = "Back"
nc2.navigationItem.backBarButtonItem = back
nc2.pushViewController(contactVC!, animated: true)
self.present(nc2, animated: true, completion: nil)
}
}
I also tried another way, I create a segue in storyboard and connect the popover to the navigation controller, the backbutton still doesn't appear.The segue is hooked up to the navigation controller in the storyboard.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let seg = segue.destination as! ContactsTableViewController
let back = UIBarButtonItem()
back.title = "Back"
navigationItem.backBarButtonItem = back
}
My question is: how to make the back button appear? After it appears, how to make it work? I don't want the back button points to the popover, I want the back button will make users return to the VC that creates the popoverVC.
The back button in a navigation bar doesn't belong to THIS view controller; it is the back button item of the previous view controller already on the navigation stack. But you have no previous view controller. So just use, like, a left bar button item that says Back, or something.
Thus, give the bar button item (back) an action and target and change
nc2.navigationItem.backBarButtonItem = back
to
nc2.navigationItem.leftBarButtonItem = back
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 am trying to create segues between my UIViewControllers but am having some difficulties with creating a segue from a UITableViewCell and a UIButton.
When I create a show detail segue through storyboard from a UITableViewCell to a UIViewController it works perfectly and I get the back button showing up. But when I try to create a show detail segue from a UIButton to a UIViewController it doesn't register the navigation stack and presents the screen modally without the back button.
How can I make a successful show detail segue from a UIButton to a viewcontroller? I am new to iOS and am having trouble determining why the UIButton segue doesn't behave the same as the UITableViewCell segue.
Thanks in advance for any help!
Dont connect a segue manually do it through button action.
This is assuming the viewController that has this button is the root view controller of the navigation controller
#IBAction func myButtonTapped(_ sender: Any) {
let vc = self.storyboard!.instantiateViewController(withIdentifier: "YOUR STORYBOARD IDENTIFIER GOE HERE")
self.show(vc, sender: self)
}
If you want to go to a tab of a tab bar controller you have to specify its index. I think you can set it in storyboard but i just go with 0 is on the left then they go up sequentially. so in the example below i want to go to the second tab of the tab bar controller with a modal transition. I assume you can use show here like example above I've just never done it.
let tbc = self.storyboard!.instantiateViewController(withIdentifier: "MyTabController") as! UITabBarController
tbc.selectedIndex = 1 // this is 2nd tab index.. start at 0
tbc.modalPresentationStyle = .overCurrentContext
tbc.modalTransitionStyle = .coverVertical
self.present(tbc, animated: true, completion: { finished in
// you can also do some completion stuff in here if you require it.
// self.view.removeFromSuperview()
// self.navigationController?.navigationBar.removeFromSuperview()
})
let vc = self.storyboard!.instantiateViewController(withIdentifier: "StoryBoardID")
self.navigationController?.pushViewController(vc, animated: true)
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.