I want to create the following user flow in my Swift application:
User clicks a button
A modal view pops over with a choice. The user selects a choice.
A second view appears with another choice (using a "show" transition, so the user can press < Back to alter choice #1). The user selects the second choice.
The second modal view segues back to the original view (which has the button).
To do this, I created the following structure:
--> NC1 --> VC1 --modal-> NC2 --> VC2 --show-> VC3 (NC = UINavigationController,
^ | VC = UIViewController)
| |
----Segue I'm trying to achieve-----
I've tried to pop all the way back to VC1 by passing a reference to VC1 all the way through to VC3, then calling the following code in VC3:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// Perform Segue
print("Trying to segue")
rootController!.navigationController!.popToRootViewControllerAnimated(true)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
where rootController is the reference to VC1. Unfortunately the segue doesn't occur; the second modal view stays on the screen, but I get the "Trying to segue" message printed.
Am I trying to implement this user flow the right way? If so, any suggestions why I can't pop to a root view in another navigation controller?
If you are using storyboards, you can do this using an unwind segue.
In VC1 implement an #IBAction method, which takes a UIStoryboardSegue as parameter:
#IBAction func unwindToVC1(sender: UIStoryboardSegue) {
}
Then from the Prototype cell of VC3 control-drag to the Exit icon of the view controller, select Selection Segue: unwindToVC1: and you are good to go.
You can achieve a similar result using code also.
Implement didSelectRowAtIndexPath in VC3 like this:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
navigationController!.dismissViewControllerAnimated(true, completion: nil)
}
Related
I am very new to Swift, and i'm wondering how to perform segue when user tap one of the table view cells.
In UITableViewCell controller I tried to type performSegue and wait for hints by Xcode like below
override func setSelected(_ selected: Bool, animated: Bool) {
if selected {
performSegue() <- HERE
}
super.setSelected(selected, animated: true)
}
I couldn't find any hints, so I think i misunderstand the concept.
Is there any way to perform segue when a cell is tapped? And I also want to send some data to the destination, so it'll be great if you can provide a way to send data to the destination as well.
Thank you in advance.
Segues In Code:
So firstly a good way of performing something when a cell (table view cell) is tapped is using the tableView didSelectRowAt method:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Place code you want to be performed when a cell is tapped inside of here, for example:
performSegue(withIdentifier: "identifier", sender Any?) // Make sure your identifier makes sense and is preferably something memorable and easily recognisable.
}
The segue identifier is just so that swift/interface builder knows which segue you are referring to.
The sender is the thing which caused the segue to be performed an example of this is a UIButton.
Next you will need to prepare for the segue inside of the View Controller which is being segued to using the following method:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Do any required setup for this segue such as passing in data loading required views and specifying how you want the segue to be performed such as if you want the view to be pushed, or presented as a popover etc... for example:
if let vc = segue.destination as? ViewController { // Downcast to the desired view controller.
// Configure the view controller here...
}
}
NOTE: You can also create segues in interface builder by control dragging from something such as a UIButton and then setting the segue identifier in the Attributes Inspector in the panel on the right:
Segues In Interface Builder:
Control drag and this menu will pop up for you to select the presentation:
Then set an identifier for the segue in the Attributes panel in the right side of the screen:
Finally prepare for the segue inside of the view controller which will be segued to in code just like you do when creating a segue in code.
This video might also be useful to you to gain more clarification on segues and how to perform and prepare for them etc:
https://youtu.be/DxCydBmOqXU
Back Button shows in StoryBoard but not Simulator.
I added a segue from TableViewController to DetailViewController to pass data. The Storyboard automatically shows a Back Button on DetailViewController, but when I run the Simulator, the button doesn't show up.
This is my Storyboard:
A closer look of TableViewController and DetailViewController:
But in my Simulator the button doesn't show up:
The hierarchy of the whole project:
I want to know where to configure the back button(in my segue?), or instead deleting the button(not letting it show in the Storyboard) so I can create one myself.
The code of my segue in my TableViewController:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "showDetailView", sender: indexPath)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
switch (segue.destination, sender) {
case (let controller as DetailViewController, let indexPath as NSIndexPath):
controller.receivedName = storeList[indexPath.row].name!
controller.receivedDesc = storeList[indexPath.row].desc!
controller.receivedRate = storeList[indexPath.row].rate
controller.receivedUrl = storeList[indexPath.row].url!
default:
print("unknown segue")
break
}
}
Your initial view controller in the storyboard is actually the TabelViewController (you can see there is an arrow to it).
Thats' why when you start the scene and the TableViewController shows but it is not embedded in the navigation because the navigation controller has never been created.
Just change which is the initial view controller to be the navigation controller or any other before the navigation controller which holds the TableViewController.
You can just drag the arrow to change the initial controller in the storyboard
I have an app that is two view controllers connected via a tab bar controller. The first view is a tableView cell and the second is several labels that are connected.
Currently my code works so that when I select a cell in the table view, it brings up the associated information from the second view controller. However, to get back to the first view controller, I currently have a back button.
How do I make it so the tab bar controller buttons don't disappear when the cell is tapped, so that I can quickly and more seamlessly jump between the two view controllers?
Relevant code for selecting a cell:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
x = indexPath.row
y = indexPath.row
z = indexPath.row
performSegue(withIdentifier: "seque", sender: self)
}
And then I call this in the viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
xLabel.text = wordList[x]
yLabel.text = wordDefinition[y]
zLabel.text = wordSnippet[z]
If I understand this correctly, you need to have your first VC embedded into a NavigationController and the Navigation Controller embedded into a TabBarController.
This way, when you drag a segue from your first VC to your second VC, the second VC will also be embedded into the same Navigation Controller.
And since the Navigation Controller is inside a TabBar Controller, the second VC will also be embedded into the same TabBar Controller.
You will end up with something like this:
As you can see, all VCs retain the TabBar and the Navigation Bar.
And you can do this by selecting the desired VC and then from Editor menu > Embed in.
I've just recently started learning about Xcode and the swift language. To get myself familiarise with them I'm trying to build a simple app with what I've learnt so far. 1
The screenshot shows what I want to achieve. VC1 and VC2 are both a navigation + tab VC. When the user taps on the "Preset" button on the navigation bar of VC1, the screen will switch to VC2 which has a table with different preset values. What I did was to create an action segue (show) from the "preset" button to VC2. All good so far.
This is what I want to do but couldn't figure a way to do it: At the table at VC2, when the user click on any row, he will be brought back to VC1 and the value in the selected row is being inserted into a textfield in VC1.
What I had tried to do was to create an action segue (show) from the table cell to VC1. But by doing so, the navigation bar and tab bar on VC1 and VC2 has disappeared 2
Is what I'm trying to do achievable? if yes, what did i do wrongly?
I tend to use storyboards with an IBAction to return to the previous view controller, such as
#IBAction func userDidSelectTableCell() {
self.navigationController?.popViewController(animated: true)
}
But in your case I think your after a unwind segue, I've not used one but you can read more about them at this following link https://spin.atomicobject.com/2014/10/25/ios-unwind-segues/.
Hope that helps
For your purpose you need to use a unwind segue. In order to achieve this, you firstly need to declare a global variable in your VC2 class which stores the value of the selected row. You can use the didSelectRow method for that.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedCell = indexPath.row
self.performSegue(withIdentifier: "YourIdentifier", sender: self)
}
you need to add a #IBAction method in you VC1 class.
#IBAction func unwindtoVC1(_ sender: UIStoryboardSegue){
if let sourceVC = sender.source as? YourTableViewController {
yourTextFiled.text = sourceVC.selectedCell
}
}
In you storyboard, in VC2, Control+drag-drop from viewController to Exit portion to create a segue.
From the pop-up that comes, select the unwindtoVC1 method.
Now, select the segue and in the attributes inspector, give it any identifier.
Use this identifier in your VC2 class, in the didSelectRow method, in the self.performSegue(withIdentifier: "YourIdentifier", sender: self)
Hope this helps.
I currently have a ViewController with prototype cells in a UITable View. The cells currently display content from a Firebase DB when loaded. What I would like to do is when a cell is pressed more information is shown from the Firebase DB. However, I currently cannot get the segue to push to the ViewController from the cell. What should I do so this would work?
Image of my Storyboard--
These are two pieces of code that will help. For swift:
#IBAction func showEntries(_ sender: Any) {
self.performSegue(withIdentifier: <sequeId>, sender: nil)
}
and a button with the action wrapping this.
For Objective C, you can perform the segue like so.
[self performSegueWithIdentifier:#"showEditor" sender:self];
Create the action by CTRL and dragging the button or cell into the corresponding code file, select action and name it.
It's hard to tell, but from the screenshot, it looks like the segue goes from controller to controller, rather than cell to controller. If you'd like to do it like this, then you need to give the segue an identifier (in the right panel of your screenshot) and then override the delegate method
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "yourIdentifier", sender: self)
}
OR (if you don't feel like going to all this trouble)...in the storyboard, drag the segue from the cell to the view controller you want to push, rather than from controller to controller.
For example,
Notice how when the segue is selected, only the cell is outlined in blue, rather than the entire view controller.
It's because cells are dynamic, so you can't assign an event handler from cell to uiviewcontroller. Instead you should make the segue inside your cellForRowAtIndexPath method and push it from there. If you want to see the segue inside the storyboard nevertheless, then you can also ctrl+drag from viewcontroller1 to viewcontroller2 to make a new segue. Then select the segue and give it a Storyboard id. After That you can call self.performSegue(withIdentifier: "yourIdentifier")
I actually found the solution to this. The issue was fairly simple actually. All I had to so was change the selection setting within the table view attributes from 'No Selection' to 'Single Selection'.