Swift - Performing Segue from UITableViewCells to different View Controller Destinations - uitableview

so I am having issues figuring out how to implement code for the following purpose:
Essentially my app allows a user on the opening screen to type different categories in a UItextfield, hit enter, and then have those categories appear in a UITableView (all on the same initial view controller). For example the user could enter "Friends from home", "College Friends", "NBA Players".
From there, I want the user to be able to drill down on one of the rows, and be taken by a segue to a UITableView controller where they can add names to those lists and ultimately be able to rank them for fun...
But my question currently is once the user has added the groups that he wants on the initial view controller, how does he/she drill down and segue to a SPECIFIC view controller based on the row he/she tapped?
(I want to post a picture of my storyboard but I don't have the necessary 10 reputation points yet!)
I tried code like this but have gotten no results. I will also note that I dragged a show segue from the uitableviewcell on the opening view controller to the UITableViewController (segue is named "EachGroup").
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "EachGroup") {
// pass data to next view
let UIViewController:ViewController = segue.destinationViewController as ViewController
let indexPath = self.tableView.indexPathForSelectedRow()
}

That's not a new question, many topics about.
You should specify a tag on each cell : cell.tag = 0
In your prepareForSegue, use :
if sender!.tag.littleEndian == 0
{
let UIViewController:ViewController = segue.destinationViewController as YourSpecificViewcontroller
}
else if sender!.tag.littleEndian == 1
{
let UIViewController:ViewController = segue.destinationViewController as YourOtherSpecificViewcontroller
}
Hope it helps,
Sidd.

Related

Unable to call embedded segue the second time

I'm trying to build an app similar to Instagram, and I'm stuck at the comments portion. On the left side of the image, it is a VC that has an embedded container view with UITextField and UIButton at the bottom. The container view is embedded with a UITableView that contains all the user profile image, username and the comment itself.
At first load, it can perfectly grab all comments for that post from server side, and displayed perfectly. However, I am unable to call the segue again using prepareForSegue to update the UITableView. I'm receiving an error:
There are unexpected subviews in the container view. Perhaps the embed
segue has already fired once or a subview was added programmatically?
Below are my codes:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "getCommentSegue"){
if let destination = segue.destinationViewController as? CommentsTVC{
if(!self.commentID.isEmpty){
destination.UpdateCommentRow(self.profileImage, commentID: self.commentID, comment: self.postComment, dateTimePost: self.dateTime)
self.commentID = String()
}
destination.postID = self.postID
}
}
}
And after successfully adding a row to my database in the dispatch_async:
self.performSegueWithIdentifier("getCommentSegue", sender: nil)
I've also noticed that when it append new comment to my existing object that stores all the comments, the count for it is 0. I believe it is taking a new reference for the object. Please help!
embed navigationcontroller to container view and then try. It may solve youar problem i think..!!
Remove your container view's subviews before you call performSegue.

How to keep user inputs within corresponding text fields after segue [Swift]

I have two view controllers, first view containing "user form" for inputs and second to display the final results. Everything seems to be working fine, i.e. I can send the final results using "prepareForSegue" function (see below)
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var SecondVC: SecondViewController = segue.destinationViewController as! SecondViewController
SecondVC.receivedDP_Summary = summaryDP
SecondVC.receivedDC_Summary = summaryDC
SecondVC.receivedDP_CA_Summary = summaryDrillPipe_CsgAnnulus
SecondVC.receivedDC_CA_Summary = summaryDrillCollar_CsgAnnulus
}
However, as soon as I return to first control view after initialising segue, all the user inputs disappear. Is there any way to store the user inputs within the respective text field so they can be manipulated to see varying results?
I am still very new to programming, so I will really appreciate any help

navigating through desired views (while skipping some views) using UINavigationBar back button

I am using a UICollectionView to show a list of images.
1)By clicking on any cell the image and some description about it is displayed in the resulting view controller(using a push segue).
2)When i swipe from the left/right edge(using PanGesture) i need to display the details of the previous/next image in the collection view.
3)But the back button of the navigation bar has to take me back to the collection view and not to the previous displayed details (shown by the PanGesture).
I know how to get 1 and 2 done but don't have a concrete idea to get the 3rd job done.
Any help would be appreciated.
You can find your desired UIViewController in your navigation stack using for loop. Try this. This is in Swift
for (var i = 0; i < self.navigationController?.viewControllers.count; i++) {
if (self.navigationController?.viewControllers[i].isKindOfClass(YourViewController) == true) {
println("is sw \(self.navigationController!.viewControllers[i])")
(self.navigationController!.viewControllers[i] as! YourViewController)
self.navigationController?.popToViewController(self.navigationController!.viewControllers[i] as! YourViewController, animated: true)
break;
}
When you navigate from one view to another (e.g. by showing a detail view of your images one after the other, then all these views are internally piled up as a stack.
Thus, if you want to jump directly to a view somewhere in between this stack (e.g. the collection view), then you can use the Unwind Segue.
In your case it should work like this:
First, in your collection view (i.e. your back button destination) you need to implement a UIStoryboard Segue as follows
#IBAction func myGoBackPoint(segue: UIStoryboardSegue) {
println("Jump directly back here from any other view")
}
Then, in the storyboard of your detail view you ctrl-drag directly to top rightmost Exit marker and choose the previously created back button destination:
In the code of the detail view implement the "go back instruction"
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "My Unwind Segue" {
if let myUnwindSegue = segue.destinationViewController as? MyCollectionViewController {
// prepare for segue
}
}
}
Hope this helps.
Finally after researching a lot i found that using a collection view with each cell taking up the whole screen is the best way to go about solving this issue.
Thankyou

Multiple unwind in the same button - Swift

I would like to know if it is possible to assign two different unwind method at the same button. For example:
I have this views navigations:
Home->A->B
Home->C->B
A and C views navigates to B, but then I want to return to previous views using the same B view and controller.
It is possible?
I have been thinking about write assign unwind method to the button programmatically depending what view comes.
Thanks in advance
I'm sorry about my english, is not good.
Here's a Swift solution that worked well for me. The code below only works if you hookup your segues correctly in the storyboard and in code. Checkout this page for great explanations on setting up unwind segues.
In summary:
You're accessing the same view from multiple other views. So, when you segue to a view, you can pass the source view controller (the view that you're currently in) to a property in the view that you're going to.
In your view that you will unwind out of, you can check the property holding the info (the class) on where you came from, and then perform a segue based on what view it is.
The code: (using ex: Home -> A -> B or... Home -> C -> B)
Note: B is the view that will unwind to multiple different views.
In A or C: (code works the same way in both views)
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "segueIdentifierInViewAthatGoesToViewB" {
let controller:B = segue.destinationViewController as! B
//the code below passes our current view controller class to a property in view B.
//So, view B will know what view we came from.
//In our example, we're coming from view A or C
controller.viewControllerNavigatedFrom = segue.sourceViewController
}
}
In B:
//setup an IBAction that will check the source view controller (that we passed when we segued to this view) and perform a segue based on where we came from. You can hook this up to a button or anything you want really.
//isKindOfClass(A) - "A" would be the name of your class
//setup a property to receive the view controller class where we are coming from
var viewControllerNavigatedFrom:AnyObject?
#IBAction func myButtonPressed(sender: AnyObject) {
if self.viewControllerNavigatedFrom!.isKindOfClass(A) {
//Unwind to view A
performSegueWithIdentifier("unwindFromBbackToA", sender: sender)
}
else if self.viewControllerNavigatedFrom!.isKindOfClass(C) {
//Unwind to view C
performSegueWithIdentifier("unwindFromBbackToC", sender: sender)
}
}
Although, question isn't very clear. But what I could understand is that you want to navigate back to the previous view i.e. B>C or B>A depending upon where user came from.
If so, then check the UINavigationController. It keeps track of the navigation history and automatically adds a back button. Kind of like the back button in our browsers.
Here is a tutorial, although a bit old: Link

Swift selecting rows in TableViewController and opening up a ViewController

I started learning App development in Swift recently and I'm still fairly new. I'm trying to make an app where at the home screen, the user will click a button called profiles, and it'll show a list of names in a tableViewController. When the user clicks a name it'll open up the person's profile in a viewController.
I'm stuck so far on what method and what to code in that method to allow the user to click on the list of names in my TableViewController and then open up a viewController for that person. I've already created my other viewController (DetailsViewController) and created a segue between the TableViewController and the DetailsViewController.
I've been searching for a long time on how to do this but couldn't find any luck. If someone could just point me in the right direction that'd be great.
You have to create the segue from the cell to the detail view controller. To provide the detail controller with the data, give it a property (e.g. name) that you can set in prepareForSegue which you override in the parent controller.
override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
let cell = sender as UITableViewCell // or your cell subclass
let indexPath = self.tableView.indexPathForCell(cell)
let name = dataArray[indexPath.row]
// or something else depending on the structure of your data model
let controller = segue!.destinationViewController() as DetailViewController
controller.name = name
}
The detail view controller can now populate its view with the passed information.

Resources