(Swift) how to use Segue pass textField to TableViewCell under another ViewController? - swift

I wonder how to pass the textField to my customTableViweCell. Please, someone help me! enter image description here
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segue" {
let nextVC = segue.destination as! UINavigationController
let dest = nextVC.topViewController as! ViewController
let ind = sender as! customTableViewCell
let nTxt = nameTxt.text?.description
let pTxt = phoneTxt.text?.description
let eTxt = emailTxt.text?.description
let dTxt = dobTxt.text?.description
ind.lb1 = "\(nTxt!)"
ind.lb2 = "\(pTxt!)"
ind.lb3 = "\(eTxt!)"
ind.lb4 = "\(dTxt!)"
}
}

segue.destination will contain the destination view controller. You should cast it to your custom view controller that will be presented. That view controller, should have properties defined that will be used to build that view. You should set those properties in prepare so that when the destination view controller loads the view, those properties can be used to update the view components. Here's an example.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segue" {
let dest = segue.destination as? YourCustomViewController
dest?.customProp = someValue
}
}

Related

Segue from an UITableViewController to another one issue

I am trying to segue 2 ViewControllers of of UI Table type, but it shows me an error "Thread 1: signal SIGABRT", here the code:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showPlates"{
if let indexPath = self.tableView.indexPathForSelectedRow{
let selectedRecipe = self.recetas[indexPath.row]
let destinationViewController = segue.destination as! PlateViewController
destinationViewController.recetas = [selectedRecipe]
}
}
}
Probably you haven't set a class (PlateViewController) for desired destination ViewController in your storyboard (I guess in Main.storyboard)

How to create segue for a view controller to another view controller which is embedded in a navigation controller?

This is how my storyboard is linked.
This is my segue function. Segue to priceViewController works fine, but to the other view controller keeps crashing. Receiving error 'Could not cast value of type 'UINavigationController' (0x11e264a20) to 'tableViewwithSections.priceViewController'.
Any help would be appreciated.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if(segue.identifier == "toPriceView"){
let des = segue.destination as! priceViewController
des.currentCity = String(self.cityLabel.text!)
}
if(segue.identifier == "toTableView"){
let des = segue.destination as! tableViewController
des.currentCity = String(self.cityLabel.text!)
}
}
You had something like this originally and as far as I can see it should work. You need to access the tableViewController through the UINavigationController.
if(segue.identifier == "toTableView"){
let nc = segue.destination as! UINavigationController
let des = nc.topViewController! as! tableViewController
des.currentCity = String(cityLabel.text!)
}

Cannot find `ViewController` in prepare for segue

I´m trying to pass data from one ViewController to another. But when I try to set the destinationVC Xcode doesn't recognize the ViewController. I get no autocomplete for the DataRecieverViewController I get autocomplete for the other ViewCOntrollers When I write it in manually, I get no errors, but no data is passed.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == passDataSegue {
let destination = segue.destination as! DataRecieverViewController //Xcode doesn't recognize this VC. I get no autocomplete.
destination.data1 = data1
}
}
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "identfiername")
{
let destination = segue.destination as! DataRecieverViewController
destination.data1 = data1
}
}
for you the identifier name is passDataSegue, When you work with segues please cross check the seguename and viewcontroller name once more to avoid erorrs.
About autocomplete try to save the file DataRecieverViewController and build your project it is a common bug of Xcode.
About data pass
Are you sure you are getting to the right instance of DataRecieverViewController, not the general class?
Is it embedded in a NavigationController ot UITabBarController ?
if Yes you have to get the right instance.
If embedded in UITabBarController
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == passDataSegue)
{
let tabBar = segue.destination as! UITabBarController
let controller = tabBar.viewControllers?.first(where: { $0 is DataRecieverViewController }) as? DataRecieverViewController
controller.data1 = data1
}
In case of UINavigationController
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == passDataSegue)
{
let NavBar = segue.destination as! UINavigationController
let NavC = NavBar.viewControllers?.first(where: { $0 is DataRecieverViewController }) as? DataRecieverViewController
let controller = NavC.rootViewController as? DataRecieverViewController
controller.data1 = data1
}

Pass image from collectionview to VC

I've tried a lot of methods i couldn't get it to work
ViewController 1 have : Collectionview > Cell > image inside the cell
ViewController 2 want to display the image which in VC 1
When you click on cell it has segue to push you to VC 2
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "popup" {
let viewController: friendpopup = segue.destination as! friendpopup
let indexPath = sender as! NSIndexPath
let nicknamex = self.nicknameArray[indexPath.row]
let usernamex = self.userArray[indexPath.row]
let photox = self.friendsphotos[indexPath.row] // the photos PFFiles i think
viewController.snick = nicknamex
viewController.suser = usernamex
viewController.sphoto = // ????
}
nickname and user works fine only the image i couldn't display it.
I tried when you click on cell it will send the image to var but isn't working
var photovar:UIImage!
didSelectItemAt( self.photovar = cell.profilepic.image)
then in prepareSegue( viewcontroller.sphoto = self.photovar)
isn't woking, Anyone could help me to fix that to display the image? Thanks
Tightening up your code a bit....
First VC
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "popup" {
if let vc = segue.destination as? friendpopup {
let indexPath = sender as! NSIndexPath
vc.snick = self.nicknameArray[indexPath.row]
vc.suser = self.userArray[indexPath.row]
vc.sphoto = self.friendsphotos[indexPath.row]
}
}
}
Second VC:
// making assumptions on variable types
var snick:String!
var suser:String!
var sphoto:UIImage!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// do work here
if sphoto != nil {
imageView.image = sPhoto
} else {
// set a default image here
}
}
Most of this is similar to your code, but one last note - if the first 2 properties are coming across correctly, check in the first VC that you are pulling the image out properly.

what is the collectionView form of indexPathForSelectedRow?

I'm attempting to create a variable "indexx" that will be segued to my next tableController. indexx will hold the indexPath of the selected cell from my collectionView, however, I am unsure which property to use. I am vastly more familiar with tableController properties so I'm having difficulty finding a successful solution to this piece of code. Would you happen to know my error?
As a quick note - foodcv is my collectionView and numberr is the variable in the second destination controller.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destViewController = segue.destination as? foodtable {
let indexx = self.foodcv.indexPath(for: foodcell)
destViewController.numberr = indexx
}
}
let numberr : IndexPath = []
Maybe it's too late, but here you go:
Swift 4
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destViewController = segue.destination as? foodtable {
let indexPaths : NSArray = foodcv.indexPathsForSelectedItems! as NSArray
let indexx : IndexPath = indexPaths[0] as! IndexPath
destViewController.numberr = indexx[selectedindexPath.row]
}
}
Use this modified implementation,
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destViewController = segue.destination as? foodtable {
let indexx = self.foodcv.indexPathForCell(sender as! UICollectionViewCell)
destViewController.numberr = indexx
}
}