I am currently implementing a simple fitness app.
I have each exercises for each tableviewcell row and when i click each tableviewcell, I want to segue it to tutorial screen where it shows short clip of video.
My question is;
If I have 100 different exercises, do i need to created 100 different segue for each exercises?
and how do I make different tableviewcells to segue into same view?
You asked "If I have 100 different exercises, do i need to created 100 different segues..."
In a word, no.
You could create a single view controller to view controller segue and give it an identifier.
Implement the tableView:didSelectRowAtIndexPath: method. In that method, save the selected indexPath to an instance variable and call performSegueWithIdentifier to invoke your segue.
In your prepareForSegue method, take the selected cell's indexPath and use it to look up the data for that cell. Cast the segue's destination view controller to the correct class, and then set a property you've defined in the destination view controller to the data you want to display for the selected cell (information about the selected exercise, in this case.)
Related
I need to navigate inside folders and files in directory (from server). The problem is that I don't know the number of folders so it's not possible to use performSegueWithIdentifier statically. How can I use navigation controller with dynamically number of view controllers in swift? I want to "push" a new view controller every time a user tap on a folder in order to list files/folders inside it and I want to do it with UINavigationController so the user have the possibility to go back with "previous" button.
Both storyboard and programmatically approaches are ok.
Thanks you
Storyboards and segues are just a crutch. Think about how you would do this without them. At each level, to go down a level, you would just instantiate a new view controller and push it onto the navigation controller stack with pushViewController:animated:.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationController_Class/#//apple_ref/occ/instm/UINavigationController/pushViewController:animated:
And in fact it takes only one view controller class to do this, since any instance can create and push another instance of its own class. The display of one folder is exactly like the display of any other.
So if you wanted to configure this notion in a storyboard, you would have a circular segue, that is, the view controller would have a push / show segue leading to itself.
I agree with #matt's answer, just create the controller and push it. For sake of completeness, you can do this in a Storyboard with a segue.
Here's how:
So that you can call the segue programmatically, add an additional prototype cell to your tableView. (You do this because you don't want the segue to be automatically triggered when the tableViewCell is selected. By using an additional prototype cell, the segue can be wired up, but it will never be triggered automatically since this prototype cell will never actually be instantiated.)
Control-drag from this prototype cell to the viewController icon at the top of the tableViewController. Select "Show" from the pop-up.
Find this segue in the Document Outline View and give it an identifier such as "showFolderSegue" in the Attributes Inspector.
Now, when you want to trigger the segue, call: self.performSegueWithIdentifier("showFolderSegue", sender: self)
You can use prepareForSegue to set up the new tableViewController as you normally would.
This method too works with a single tableViewController.
I just started iOS development last week and I am creating a Table View based application. I having trouble understanding how to use storyboard.
I want each Table Cell to open a different ViewController.
Currently it is setup like this:
Then in the Component View Controller I use if/else statements to determine what content to load. The problem occurs when one of the views needed a TabBar.
How do I assign different View Controllers to each individual cell, rather than one "template" view and forced to add everything dynamically.
First off, I will say that it probably isn't the best idea to assign an individual view per cell. However, if it is what you wish to do, then so be it.
What you would do is create a segue from the table view to the new view by clicking on the table view icon and dragging a segue like so:
2.You would give that segue an identifier like "embedTweetsSegue" or something.
You can then check for the cell being touched and perform the segue programmatically using:
performSegueWithIdentifier("embedTweetsSegue", sender: self)
I have the set up of a user clicking a UITableViewCell, this triggers a segue to a 'detail' view controller which is popped onto the stack.
First,
User Taps -> [myVC tableView didSelectRow...] - this is where I can work out which cell and therefore which model object my user wants to modify or access.
Second,
User Taps -> [myVC prepareForSegue...] - this is where I set up my detail view controller with the correct model object.
So, do I just store the selected model object in an instance variable between the two functions being called?
In prepareForSegue, the sender argument will be the cell, so you can work out which one was tapped at that point. You could also check the tableview's selectedIndexPath property.
You don't need to implement both functions.
I understand that you need to pass an instance of your model to your details view controller after you tap on a table view row. You have some ways to achieve that:
You can create a property in your view controller with a data type of your model. Let's name it for example firstObject. Then, inside didSelectRow you can set this property or update it according to your needs. Then, inside prepareForSegue you can pass this instance to your details view controller, by doing something like: detailsVC.detailsObject = firstObject.
Inside prepareForSegue, you can get the value of the current tapped table view row using indexPathForSelectedRow.row. Assuming that you have an array holding different objects of your model that you need them to be sent to your details view controller based on the tapped table view row index, you can do something like:
detailsVC.detailsObject = myObjectsArray[myTableView.indexPathForSelectedRow.row].
I hope that I was able to make it more clear for you.
Ok, I haven't been able to find a definitive answer to this. I'm on Xcode 6 working with Swift and a UISplitView. I have multiple detail views in storyboard and I want to be able to replace my detail view with another when selecting from a tableview. In Xcode 5, I was able to drag multiple segues from my prototype cell and use performSegueWithIdentifier. In Xcode 6, I can only have one segue coming from my prototype cell.
What's the best way to go about this?
One type of cell, one segue. This seems to be a logical and useful system constraint.
One solution is to just create more cell types with different cell identifiers.
Alternatively, if you have another way to determine which segue to use, draw all segues from the view controller instead of the cell. You can attach the necessary row object information in the sender object when calling the segue from the cell selection method.
You can create as many segues as u want from viewcontroller and give each seuge an different segue Identifier in IB, and the call each with performSegueWithIdentifer method
i have a table view with dynamic prototype cells divided into 2 different sections named "Forums" and "Threads". When i click on a tableview cell from the Forums section, i want to transit to the same page with a different data to display while if i click a cell from the Threads section, it should open a different scene. In short, two different types of transition segues from 2 different tableview sections)
Can anyone please help me with this?
Make sure the table view controller is embedded in a navigation controller.
Hook up two push segues to the table view controller (not to the cells).
Give those segues appropriate identifiers in the storyboard using the attributes inspector tab on each segue.
In -tableView didSelectRowAtIndexPath: add an if statement to detect which section the row tapped was in.
In the branches of the if statement, call -performSegueWithIdentifier on your controller using the identifier of the appropriate segue .
If you need to set up anything in the view controller you're seguing to from the table view, override prepareForSegue: sender: in the table view controller.
Very helpful, it worked for me, but I had to used BOTH didSelectRowAtIndexPath AND prepareForSegue, while I was reading in other posts that this is not very good.
I read that we need to use only the prepareForSegue if we need to set up any stuff...
Do you think that I should continue with both of them?
Thanks!