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.
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 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'.
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.
I have a tableview with cells which trigger a segue when selected, taking me to the next view. Everything happens perfectly if I put break-points in the code.
TableView is not the first View in the app. It's is loaded via a segue from the Main Screen of the app via an on-screen button, which surprisingly doesn't exhibit this behaviour(single click works fine first time itself).
However, I need to click a cell "twice"(time difference between the clicks doesnt matter) to select it first time when there are no break-points. Thereon, single clicks are fine. Testing this on my Xcode iPhone simulator.
My Code -
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// Keep track of the Holding selected
self.selectedHolding = self.portfolio.holdings[indexPath.row]
// Trigger the segue to the detail view
self.performSegueWithIdentifier("toHoldingTransactionsSegue", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let actualIdentifier = segue.identifier {
if actualIdentifier == "toHoldingTransactionsSegue" {
let holdingTransactionsViewController = segue.destinationViewController as HoldingTransactionsViewController
holdingTransactionsViewController.holding = self.selectedHolding
holdingTransactionsViewController.delegate = self
}
}
}
Please help.
You should connect the segue to the view controller instead of to the cell. When you connect the segue to the cell directly it bypasses / interferes with didSelectCellAtIndexPath