Segue from a Custom Cell in UItableVew in IOS - ios

I have created a custom cell and want to perform a segue to another ViewController by clicking on it.
I used didSelectRowAtIndexPath method and performed a segue but no result. The segue does not work!
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("toDetailViewSegue", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let indexPath = tableView.indexPathForSelectedRow;
let cellname = tableView.cellForRowAtIndexPath(indexPath!) as! CardTableViewCell;
let DetailViewController = segue.destinationViewController
DetailViewController.title = cellname.textLabel?.text
}

Check the following items:
First, make sure that you established your segue. In your table view controller, control click on the yellow icon at the top of the table view controller's scene in the storyboard. Drag it to your destination view.
Second, make sure that the name of your Storyboard Segue matches the desired value (toDetailViewSegue).
In your storyboard, click on the segue that you created (either in the Document Outline or directly on the storyboard).
In the Utilities bar (right-side pane), go to the Attributes Inspector. There you will see Identifier of the Storyboard Segue. Make sure that it is the correct value.
Third, make sure that your custom table view cell has User Interaction Enabled checked.
In your storyboard, click on your custom cell.
Go to the Attributes Inspector. Look for the Interaction field within the View section. Make sure that it is checked.

Related

How to perform segue when a cell is selected?

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

Swift: Back Button shows in Storyboard but not Simulator

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

Segue won't show the ViewController when pressed

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'.

How to pass selected row data to another ViewController

I have a tableview like this
I want to take the link and pass it to another controlview.
Here what i have done
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let linktopass = classes[indexPath.row].valueForKey("link")
print(linktopass)
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let thirdVC: web = segue.destinationViewController as! web
thirdVC.link = linktopass as! String
But it does not work.
Here is my print's output.
Optional( http://blablabla )
If think you forget to performsegue in didSelectRowAtIndexPath.
prepareForSegue is get called before you performsegue. if you are not performing segue then your prepareForSegue will not called.
so, in didSelectRowAtIndexPath, add code like,
performSegueWithIdentifier("goToNextVC", sender: self) //your segue identifier here
//goToNextVC is segue identifier set from attribute inspector from storyboard
Make sure you have set segue identifier from attribute inspector.
You can set identifier like,
select segue - click attribute inspector - write identifier in it - press enter
Hope this will help :)

How to run a segue on itself OR another viewController

I have a main TableViewController. When the user tap on a cell in TableView, If a condition statement is true, show the main TableViewController again(but show another data on screen), and if the condition is false, show another ViewController.
In the prepareForSegue method I have this:
if segue.identifier == "identifier1"
{
let destination = segue.destinationViewController as? mainViewController // itself
destination!.x = something // the x variable is in this view controller
}
else if segue.identifier == "identifier2"
{
let destination = segue.destinationViewController as? viewController2
destination!.y = something
}
Also, to run segue after tap on cells in tableView I added this:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
if condition is true {
self.performSegueWithIdentifier("identifier1", sender: self)
}
else{
self.performSegueWithIdentifier("identifier2", sender: self)
}
}
Storyboard segue needs an identifier... It can just have one value! identifier1 or identifier2
But I want to set the identifier programmatically.
How should I set the identifier in Storyboard segue?
Is there another way to do this?
Thanks!
The destination view controller is set by the segue. Since you have two different destinations, you need two different segues in your Storyboard.
The trick is to wire the segues from the viewController icon at the top of tableViewController to each of the destination view controllers. Click on the resulting segue arrows and set their identifiers in the Attributes Inspector.
Then you can call them with performSegueWithIdentifier as you have done in your code.
Don't invoke a segue directly from the cell tap. Use an IBAction method (probably in `didSelectRowAtIndexPath, like you show in the second block of code.
That second block of code DOES set the segue identifier programmatically.
You can't switch segues in prepareForSegue.

Resources