I'm attempting to create 2 different default view controllers that then get called and do their specified action depending on what was in the cell. Would you create the view controller in the main storyboard use an xib, etc?
Basically I have a xml file that I'm parsing and then creating table views for till I get to the last (details) page for the item. From my understanding it is better to use the same tableviewcontroller multiple times instead of creating 1 for each level. Am I supposed to be creating a segue loop?
There are checks currently in place to make sure I use the right segue.
Would you pefrom the segue with:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath as IndexPath)
tableView.deselectRow(at: indexPath as IndexPath, animated: true)
segueLocation = hNTUL[(indexPath as NSIndexPath).row].locationLevel
segueTitle = hNTUL[(indexPath as NSIndexPath).row].locationTitle
if segueLocation == "TableView" {
performSegue(withIdentifier: "defaultDetailSeague", sender: cell)
let destinationVC = DefaultDetailViewController()
destinationVC.detailTitle = segueTitle
}
if segueLocation == "DetailView" {
performSegue(withIdentifier: "defaultTableSeague", sender: cell)
let destinationVC = DefaultTableViewController()
destinationVC.tableTitle = segueTitle
}
}
It is also possible that I'm trying to go about this the wrong way.
I've also been searching for project examples but haven't found any that might guide me in the right direction.
Thoughts?
If you call performSegue, you must not create a destinationVC; the segue creates it for you. To configure the destination view controller, implement prepare(for:sender:) and fetch the segue's destination, casting it down to a DefaultDetailViewController. Be sure to check that this is the right segue first!
Related
I'm currently making a to do list app using Swift 4. The home view controller has a tableview with some categories in it and when one is selected, it takes the user to a view controller where the items in that category are listed. I have a bug however as only the most recent item is showing in the list.
I think this is due to the way I am navigating to the list view controller. I am currently doing this:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let destinationVC = ListVC()
if let indexPath = tableView.indexPathForSelectedRow {
destinationVC.selectedCategory = categoryArray[indexPath.row]
}
navigationController?.pushViewController(destinationVC, animated: true)
tableView.deselectRow(at: indexPath, animated: true)
}
And in the list view controller, I just have this to load the data:
var selectedCategory : Category? {
didSet {
loadItems()
}
}
I firstly created this app using storyboards and when using segues, it worked completely fine.
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "goToItems", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinationVC = segue.destination as! TodoListVC
if let indexPath = tableView.indexPathForSelectedRow {
destinationVC.selectedCategory = categoryArray[indexPath.row]
}
}
So basically, the problem is that in the secondary list view controller, it will only show the most recently added item and no other ones even when they are stored in core data. I think it is to do with the way I am showing the secondary view controller as I am creating a new object every time.
How to properly go to the next view controller?
Remove the segue and add the storyboard id
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "storyboard_id") as! TodoListVC
vc.selectedCategory = categoryArray[indexPath.row]
self.navigationController?.pushViewController(vc, animated: true)
}
Try this it will help you:-
You can send data from one view controller to another using storyboard
instance.
let next = self.storyboard?.instantiateViewController(withIdentifier: "NextControllerStoryBoard_id")as! NextController
next.str = "data which you want to pass"
self.navigationController?.pushViewController(next, animated: true)
here NextController is your controller class name where you want to go.str is the string name which you declare on NextController like
let str = String()
you are able to send string in that variable in same way you send any thing array dictionary ,image, Int value etc.
NextControllerStoryBoard_id is id which you declare at storyboard of that controller
In storybard id add your storybard id
Hope this will help you
I think that with this chunk of code I already sensed that you are passing data to the other view controller the incorrect way:
let destinationVC = ListVC()
if let indexPath = tableView.indexPathForSelectedRow {
destinationVC.selectedCategory = categoryArray[indexPath.row]
}
...
What I would suggest is that, instead of passing the data this way, you have to pass an array containing the items within the selected category using an array, then pass that array via the prepare for segue.
Then from the viewdidappear or viewdidload method in the receiving view controller, use the passed array from the source VC and use that as a datasource for your table view within that 2nd VC.
EDIT 2: Added a half-solution at bottom. Still open to a full solution
EDIT 1: Added images.
I need help fixing navigation between a second and third tableview.
I have a Navigation Controller, and three table views. The first two show up fine such that clicking a dynamic tableview cell > in the first tableview moves to the second tableview. However the second tableview cell chevron > while set in the storyboard (Accessory: Disclosure Indicator) doesn't show up during runtime, and therefore clicking on it does not move to the third tableview. I need help fixing that.
In each case I Ctrl+linked the cells to the next tableview in storyboard as show, and have prepare for segue in the previous tableview controllers. However only one works and the other seems orphaned.
If you could suggest a checklist in making sure the Navigation Controller works across three tableviews that would be helpful. Not sure what code to show so I'll post both segues. But it might not be a segue issue so I don't know.
first tableview Weeks works going to second tableview Leagues. Loads json file depending on what is clicked.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toLeagues"
{
let leaguesController = segue.destination as! LeaguesViewController
// pass the selected row
let selectedRow = self.tableView.indexPath(for: sender as! UITableViewCell)!.row
if selectedRow == 0
{
leaguesController.weekFileName = "sports_week_1"
}
else
{
leaguesController.weekFileName = "sports_week_2"
}
}
But second tableview Leagues doesn't work going to third tableview Games
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toGames"
{
let gamesController = segue.destination as! GamesViewController
// pass the selected row
let selectedRow = self.tableView.indexPath(for: sender as! UITableViewCell)!.row
gamesController.gameName = selectedRow.description
}
}
EDIT 1: Added images:
Starts off fine in first tableview...
Stops here in second tableview, can't move to third tableview
EDIT 2: Half solution
I found a half solution. Menu item now opens new view, only that the chevron appears after the fact. didSelectRowAt adds the missing disclosure indicator directly and goes to the new view controller. Couldn't find a viewWillAppear with IndexPath so I opted for didSelectRowAt. Works when clicked at least. Just the disclosure indicator missing on initial load. How to load the accessoryType before the view runs?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
cell?.accessoryType = .disclosureIndicator
// force menu to move to GamesViewController
let myGamesView = self.storyboard!.instantiateViewController(withIdentifier: "gamesView")
self.navigationController?.pushViewController(myGamesView, animated: true)
}
Try setting accessoryType in cellForRowAt instead of didSelectRowAt.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "yourIdentifier") as! YourCustomCell
//Your other code
cell.accessoryType = .disclosureIndicator
return cell
}
i got an app without storyboards, all UI creation is made in code and I got a splitView which I would make it usable on iPhone, because as the app as been first designed for iPad only, so that when you select a row in the list in the Master view it does nothing on iPhone but is working fine on iPad.
So my question is can I create and perform the segue that allows to show the Detail View on the didSelectRowAtIndexPath method ?
Here's what i've done so far :
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let segue = UIStoryboardSegue(identifier: "test", source: self, destination: detailViewController!)
performSegueWithIdentifier("test", sender: self)
}
but when running and selecting a row the app was crashing telling it needed a performhandler so i added this :
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let segue = UIStoryboardSegue(identifier: "test", source: self, destination: detailViewController!, performHandler: { () -> Void in
let object = self.fetchedResultsController.objectAtIndexPath(indexPath)
let controller = self.detailViewController!
controller.detailItem = object
controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
controller.navigationItem.leftItemsSupplementBackButton = true
})
performSegueWithIdentifier("test", sender: self)
}
and now when selecting a row xcode says that there is no segue with such identifier "test".
I also tried to call it by segue.perform() and add the performHandler content into the prepareForSegueMethod :
if segue.identifier == "test" {
if let indexPath = self.tableView.indexPathForSelectedRow {
let object = self.fetchedResultsController.objectAtIndexPath(indexPath)
let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
controller.detailItem = object
controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
controller.navigationItem.leftItemsSupplementBackButton = true
}
}
and it does just nothing, doesn't crash, just highlight the row i selected and that's all
Can you guys help me ?
EDIT : As Oleg Gordiichuk said, it's not possible to do what I want to do without Storyboards, so thanks for his help :)
Segue it is component of the storyboard interaction it is possible to understand from name of the class UIStoryboardSegue. It is bad idea to create segues programmatically. If i am not making mistake storyboard creates them for you.
For solving of you're issue try to use some common ways like simply present ViewController.
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("id") as! MyController
self.presentViewController(vc, animated: true, completion: nil)
As i understand from our conversation in comments. You would like to create navigation for tableview to details view using segues without storyboard. For now it is impossible to do this without storyboard.
For future learning try to investigate this information.
One way is to use the didSelectRow method for tableView
Swift 3.x
// MARK: - Navigation & Pass Data
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Selected Row \(indexPath.row)")
let nextVC = YourNextViewController()
nextVC.YourLabel.text = "Passed Text"
nextVC.YourLabel.text = YourArray[indexPath.row]
// Push to next view
navigationController?.pushViewController(nextVC, animated: true)
}
Steps taken:
Created new single view project
Deleted default view controller
Added new Table View Controller
Embedded TVC into a Navigation Controller
Added a child TVC
Added custom class files and associated to each TVC
Created a Show segue from the first TVC to the child
Implemented required methods, #of sections, # of rows, cellForRowAtIndexPath
All of the tutorials I have watched and read online only include the steps I show above, and the segues start working fine. However, in my case I can not get it to work until I add the following:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("detail", sender: tableView)
}
I'm totally fine with implementing the didSelectRowAtIndexPath method if it is required. I'm just wondering if I am missing something, because it seems to work automatically in everything I've seen online.
Thanks in advance!
If you click on the segue (storyboard) and on the side menu you have to assign the text "detail" from
performSegueWithIdentifier("detail", sender: tableView)
on the Identifier field.
Like the image below
Edit:
Try the code above, rembember to change NAME_OF_THE_DETAIL_VIEW_CONTROLLER to your Detail ViewController.
In this code we set i set "self" before the performSegueWithIdentifier. And when the button is clicked you set the segue.destinationViewController to your new ViewController.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("detail", sender: indexPath);
}
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "detail") {
var detailView = segue.destinationViewController as! NAME_OF_THE_DETAIL_VIEW_CONTROLLER
}
}
Thank you all for your contributions. I figured out that the problem was the following was missing from the cellForRowAtIndexPath tableView function.
let cell = self.tableView.dequeueReusableCellWithIdentifier("custom", forIndexPath: indexPath) as! CustomCell
I had this line commented out, mostly because I have no idea what it does. :)
So I have a table view that I want to segue to specific views when a specific cell is selected. For example, if the cell at index 0 is selected, I want the "RedViewController" to be visible.
The example I keep receiving looks something like this (located in the VC with the table view)
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
if let destination = segue.destinationViewController as? RedViewController {
if let blogIndex = tableView.indexPathForSelectedRow()?.row {
destination.blogName = swiftBlogs[blogIndex]
}
}
}
}
(Where blogName and swiftBlogs are random examples)
But this just loads specific data into a singular view controller. Preferably I want a switch statement for each index path that makes a specific VC visible.
Ok, so write your code that way. Don't link your table views directly with a segue. Instead write code in your didSelectRowAtIndexPath.
In that method you can write a switch statement, or use an array lookup, to load a view controller using a unique ID and the instantiateViewControllerWithIdentifier, or trigger a through code using performSegueWithIdentifier.
I figured it out thanks to Duncan C and a little more research!
In the table view VC I added the handy function
func switchToViewController(identifier: String) {
let viewController = self.storyboard?.instantiateViewControllerWithIdentifier(identifier) as! UIViewController
self.navigationController?.setViewControllers([viewController], animated: false)
}
And in the didselectRowAtIndexPath
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let selectedRow = indexPath.indexAtPosition(indexPath.row)
switch selectedRow {
case 0:
switchToViewController("RedVCIdentifier")
default:
println("Unknown Cell selected")
}
}
But also had to add a Storyboard ID to the RedViewController in the storyboard as "RedVCIdentifier"