Segue won't show the ViewController when pressed - ios

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

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

Segue of Table cell in a tab and navtigation view controller

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.

Segue from cell in collectionViewController that has parent navigationController

I am looking at using a collectionView for a menu within an app. I want the menu to have a navigation controller so the user knows they're in the menu and can go forwards and back between the menu and the page selected from the menu.
However as i have created the collectionView programatically i don't seem able to use a 'show' segue as the cells are created at runtime.
Interface
Runtime
Is there a way of still utilising the navigation bar for this menu?
I was previously using a static tableview with segue from each cell to desired page. Ive got a bunch of code tied into each named segue so am reluctant to change.
Code using segues on the old tableview
class MenuTable: UITableViewController {
weak var centerButtonDelegate: ManageCenterButtonDelegate?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segueTour" {
fadeOutDelegate()
}
I think you should perform the segue programatically if you are linking a UICollectionViewCell to a new ViewController, they are way more easy to control, might be as well that you set it wrong in the storyboard too.
In your tableView delegate method 'selectedRowAt: IndexPath' you should do a switch statement and perform the segue there. Something like this:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
switch indexPath.item {
case 0:
//perfrom segue
performSegue("mySegue", sender: self)
default:
break
}
}
When the cell is selected or tapped (or however you want to detect this), create the next view controller and configure it appropriately to that cell, and call show.

Segue from a Custom Cell in UItableVew in 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.

Segue.destinationViewController is set to value of sourceViewController

I'm learning about iOS as I write an application that uses a UITableViewController and Core Data to display a list of items, and another view controller that also uses Core Data to configure a variety of additional attributes that are not shown in the table view.
A button in the UITableViewController's navigation bar segues to another view that lets me set a variety of details. This is done using a storyboard "Show Detail" segue, and it works fine.
I want to use the accessory action button in a UITableViewCell to segue to the same detail view controller, so I created another "Show Detail" segue in the storyboard that connects the accessory detail button to the detail view controller (ie: the same kind of segue as the button in the navigation bar)
Confusingly, when I click the detail accessory button at run time, nothing happens, and prepareForSegue is never called.
The UITableView cell style is Subtitle, and I've configured it to have a Detail accessory action. However, the accessory button isn't displayed unless I specifically add it:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: Storyboard.reuseID)
let tone = toneTable[indexPath.row]
cell.textLabel!.text = tone.valueForKey("toneName") as? String
cell.detailTextLabel!.text = tone.valueForKey("toneDescription") as? String
cell.accessoryType = UITableViewCellAccessoryType.DetailButton
return cell
I finally gave up, and called prepareForSeque myself:
override func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) {
let tone = toneTable[indexPath.row]
self.performSegueWithIdentifier("ShowToneDetail", sender: tone)
}
(I'm using the sender parameter as a convenient place to pass an NSManagedOject)
Now, prepareForSegue gets called, but if I put a breakpoint in prepareForSegue, I see that segue.destinationViewController points to the the source view controller, not the destination view controller. This is unfortunate, because I'd like to pass some values to the new instance of the destinationViewController.
Interestingly, the segue does correctly instantiate the destinationViewController, and passes control to it.
But, I don't understand what's going on. I don't know why I have to call prepareForSeque myself, and I don't know why the segue's destinationViewController isn't set correctly.
What am I doing wrong?
ps: is it just my perception, or is XCode less mature than other IDEs like Eclipse?
You're creating your cells in the wrong way. You should be using dequeueReusableCellWithIdentifier:forIndexPath: to dequeue a cell using the identifier you setup in the storyboard. By doing it the way you are, you're directly instantiating a cell that "knows" nothing about the cell in the storyboard where you added the detail disclosure button, or the segue you made.
I have no idea why the segue.destinationViewController would point to the source view controller. My guess is that it doesn't, and maybe you have your controller classes mixed up.

Resources