Storyboard:
[Navigation Controller] -> [ViewController1] -> [ViewController2]
Both ViewController1 and ViewController2 have Tables Views in them with TableViewCell.
I have control-dragged from TableViewCell of ViewController1 to ViewController2.
ViewController1.swift has following function:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("showview2", sender:self)
}
When application runs, i can click on row on ViewController1 and ViewController2 shows up. It has a Back button at the top left. BUT, it does not work, nothing happens when i click Back button.
Instead of Ctrl+Drag from TableViewCell you should do it from ViewController. I tried to reproduce the problem from your description.
What I found is ViewController2 gets pushed twice once from the Storyboard and second time because of self.performSegueWithIdentifier("showview2", sender:self)
My suggestion:
Instead of adding from TableViewCell do it from ViewController.
You can add it by Ctrl+drag from ViewController to ViewController2
See the image below
Hope this works!
Related
I am developing an iOS application in Swift.
I have create a UICollectionViewController. I have filled the UICollectionViewCells with images. How can I connect the cells individual with the different ViewController for example if i tap the first cell they will show the next ViewController (but the new ViewController need no data from the cell the content is completely different. I will only that they open the next view controller and this for every image in the UiCollectionView.
With prepareforsegue I have the problem that when I tap on every image that the same ViewController is showing. I want that every image open a different new ViewController. Which function does I need
Segues define the viewController they are transitioning to. If each cell needs to go to a different viewController, then you need a segue for each one.
Wire your segues from the viewController icon at the top of your UICollectionViewController and not from the prototype cell:
Give each of these segues a unique identifier in the Attributes Inspector:
In collectionView(_:didSelectItemAtIndexPath:), select the correct segue identifier using indexPath and call performSegueWithIdentifier(_:sender:):
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let segues = ["goToVC1", "goToVC2", "goToVC3"]
let segueID = segues[indexPath.item]
performSegueWithIdentifier(segueID, sender: self)
}
I have a TableView with 4 static cells, and each cell has a disclosure indicator. The disclosure indicator should fire a segue. This works fine, but only for the first cell. I cannot figure out how to wire up a segue for anything but the first cell, either programmatically or via the story board.
You can use the UITableView delegate like this:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// row 1 corresponds to the second cell.
if indexPath.row == 1 {
performSegueWithIdentifier("MySegueID", sender: self)
}
}
In the storyboard, create the segue from the first view controller (not from the cell itself) to the second view controller, and set its identifier to MySegueID``.
I have a Tab Bar Controller setup. When the user selects a tab, it takes them to a UIViewController. Currently I have a UITableView setup in this View Controller. What I would like to do is, upon selecting a cell from TableView1, I'd like to have a "push" effect where TableView2 comes in from the right side of the screen, and take over.
I've found the following question, similar to mine:
UITableView segue within the same ViewController
One of the suggestion is exactly what I want to achieve, however I could not get it to have that slide effect.
What I've done is added two UITableViews, then tableView1.hidden = true in viewDidLoad.
Then in code:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell: UITableViewCell!
if tableView == tableView1
{
// Dequeue the cell to load data
cell = tableView.dequeueReusableCellWithIdentifier("ID1", forIndexPath: indexPath)
....
}
else if tableView == tableView2
{
// Dequeue the cell to load data
cell = tableView.dequeueReusableCellWithIdentifier("ID2", forIndexPath: indexPath)
....
}
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
if tableView == tableView1
{
tableView2.hidden = false
tableView1.hidden = true
playlistVideosTableView.reloadData()
// Deselects the row
tableView1.deselectRowAtIndexPath(indexPath, animated: true)
}
}
However, it doesn't have that "slide effect" and makes the tableview appears instant.
I could make another UIViewController with the second tableview but I don't want to make my Tab Bar disappear.
How can I achieve this, and how can I ensure the ensure gets back to the first UITableView?
Thanks
I would recommend to simply create another UIViewController that has a UITableView, so preferably a UITableViewController.
Then in tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) you call a segue to the new ViewController. This will give you the desired effect and is a cleaner solution as well.
You will not lose your TabBar either with this solution if you embed all ViewControllers in this tab into a NavigationController.
EDIT: sample code
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("editSpecialStage", sender: tableView.cellForRowAtIndexPath(indexPath))
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let indexPath = self.tableView.indexPathForCell(sender as! UITableViewCell)!
let secondViewController = segue.destinationViewController as! SecondViewController
}
And in the Storyboard you have a NavigationController as the first ViewController from the TabBar. Then the first ViewController and then the second, connected via push segue.
I could make another UIViewController with the second tableview but I don't want to make my Tab Bar disappear.
Why will your tab bar disappear? It wouldn't if you used a UINavigationController!
Just do what I say, alright?
First create that new view controller and connect the two VCs with a show segue
First VC -show-> Second VC
Now the whole picture would look like
Tab Bar VC -view controllers-> First VC -show-> Second VC
Now modify this to
Tab Bar VC -view controllers-> Navigation Controller -root vc-> First VC -show-> Second VC
Now the segue will slide the Second VC to the left and your tab bar won't disappear!
I have a table view with a few different prototype cells. I want to have one prototype cell segued to one controller and the rest should not be clickable i.e. no seg.
Right now, I have one button linked to a controller successfully but when I tap on the tableview prototype cell which is segued to a different controller (in the storyboard) nothing happens. I don't get any error either.
What is the best way to go around this? Can I make two different segues in the storyboard? Or do I need to implement something in the tableview method did select row at index path, somehow grab the class associated with the row clicked and programmatically segue to a different screen? Something like:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("here")
//self.performSegueWithIdentifier("profileSeg", sender: self)
}
I am not getting the println here when I select a cell and yes I have assigned the delegate in viewdidload.
EDIT
I've just realised I made a mistake when explaining the question. I actually have a view at the bottom which links to one view controller. It is not a tableview row... This is the seg that works. But I have ctrl dragged to from one type of cell to a second controlleR and that seg is not working. There are also 3 other types of cells none of which have segs attached. Is this anything to do with the issue?
If didSelectRowAtIndexPath not working, it can be caused by either your UITableViewDelegate has not been set, or, it might possible that Selection of your UITableView has been turned off, need to Turn on, see image to find how.
Cheers.
Don't segue directly from cell to 'ViewController', you must segue cell 'ViewController' to the other 'ViewController'. You can segue many times from 'ViewController'.
And please add this code
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("You selected cell #\(indexPath.row)!")
if indexPath.row == 0{
performSegueWithIdentifier("profileSeg", sender: self)
}else{
performSegueWithIdentifier("anySeg", sender: self)
}
}
There are two ways to do what you want:
First if you only want to link ONE TVCell with one ViewController you could do what you started to do: Link every TVCell with the ViewController you want to go from there. For that hold ctrl and drag and drop from the prototype to the ViewController. So every TVCellPrototype can have one segue and if you hit the cell it will automatically perform the segue.
This is also the reason why your code don't prints "profile seg".
In this case you don't need to give the segue an identifier.
The second way is better if you need to have MULTIPLE segues from one TVCell to 2 or more ViewControllers. For this you link the ViewController of the TVCells with the ViewControllers you want to go. That way you can set as many segues as you want but you have to give them an identifier because you have to call them in the tableView(... didSelectRowAtIndexPath) function. (click on the segue in interface builder to give it an identifier). This will look like:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("ProfileSeg", sender: AnyObject?())
}
Now lets talk about why you don't get the println("here"):
There has to be a mistake according the function because there is no "override" in front of it. In general the compiler won't build your App because it is missing. I don't have all the code so i can't exactly say whats your mistake.
I have a tableView with identical cells. After selecting one of them I would like to open another ViewController.
I tried this code:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) {
let destination = EditViewController()
navigationController?.pushViewController(destination, animated: true)
}
but it didn't open another ViewController.
Can You give me a hint?
Just go inside you storyboard, Select you initiate view controller, Then click on Editor > Embedded In and select Navigation controller, Then Your code will be work. This mean you have set the navigation stack for your view controllers from initial view controller.
Most probably you just don't have the navigationController there - use rather present to show the viewController:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) {
let destination = EditViewController()
self.present(destination, animated: true, completion: nil)
}
Or if you want the viewController to be pushed, make sure that you embed the tableViewController into a UINavigationController.