pass NSArray to next viewcontroller in swift - swift

New to swift, hope I will get my answer. I need to pass NSArray to next view controller and in random order when button is pressed. I have UILabel and one variable declared in second view controller and segue seems working. Only peoblem is it does not pass any value. Here is my code.
#IBAction func ressedbutton(_ sender: Any) {
let quotes: NSArray = ["Quote 000", "Quote 001", "Quote 002"]
let range: UInt32 = UInt32(quotes.count)
let randomNumber = Int(arc4random_uniform(range))
let QuoteString = quotes.object(at: randomNumber)
// self.resul.text = QuoteString as? String
performSegue(withIdentifier: "showresult", sender: QuoteString)
let myVC = storyboard?.instantiateViewController(withIdentifier: "result") as! result
myVC.stringPassed =
(QuoteString as? String)!
}
and code for second view is
class result: UIViewController {
var stringPassed = ""
#IBOutlet weak var ResultLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
ResultLabel.text = stringPassed
I think my problem is in myvc.stringpassed=...? How to pass the arry I have and in randomly?
Thanks in Advance.

What you need to do is pass that value with sender in performSegue and after that access that value in prepare(for:sender:) and pass the value to destinationController.
performSegue(withIdentifier: "showresult", sender: QuoteString)
Now use this sender argument in prepare(for:sender:)
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? result, let data = sender as? String {
vc.stringPassed = data
}
}

You have to use prepare(for segue: UIStoryboardSegue, sender: Any?) method to assign data to toViewController.UIStoryboardSegue object segue have properties like identifier ,destination and source.
In this method first you check the identifier,if it matches cast the destination controller to your custom view controller and assign the data to the view controller object.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showresult"{
if let toViewController = segue.destination as? result {
vc.stringPassed = QuoteString
}
}
}

try this way remove segue give Storyboard ID to your view controller where u want to pass data and paste that same Storyboard Id below (YourIdentifier)
#IBAction func ressedbutton(_ sender: Any) {
let quotes: NSArray = ["Quote 000", "Quote 001", "Quote 002"]
let range: UInt32 = UInt32(quotes.count)
let randomNumber = Int(arc4random_uniform(range))
let QuoteString = quotes.object(at: randomNumber)
// self.resul.text = QuoteString as? String
let vc = storyboard?.instantiateViewController(withIdentifier:"YourIdentifier") as! YourNextViewController
vc.stringPassed = (QuoteString as? String)!
self.present(vc, animated: true, completion: nil)
}

Related

Pass date data from a Picker to another view controller in Swift

I want to be able to choose a time via a picker on one view and pass that data onto another view.
I am able to do this as a string but how do I pass the time data in order to do calculation.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segue1" {
let destinationController = segue.destination as! SecondViewController
destinationController.timeLabelTextOut = timeLabel.text!
}
}
#IBAction func dayFinishTimePicker(_ sender: Any) {
let timeformat = DateFormatter()
timeformat.dateFormat = "HH:mm"
let finishTime = timeformat.string(from: dayFinishTimePicker.date)
timeLabel.text = finishTime
}
Pass the date itself and format it there to get the time inside viewDidLoad of the next vc
destinationController.dateSent = dayFinishTimePicker.date
Firstly i would declare finishTime var outside the function and make it a var:
var finishTime = Date()
In SecondViewController i would create a var fTime like that:
var fTime = Date()
Then i would write in prepare func:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segue1" {
let destinationController = segue.destination as! SecondViewController
destinationController.fTime! = self.finishTime
destinationController.timeLabelTextOut = timeLabel.text!
}
}
And the #IBAction function goes like that:
#IBAction func dayFinishTimePicker(_ sender: Any) {
let timeformat = DateFormatter()
timeformat.dateFormat = "HH:mm"
self.finishTime = dayFinishTimePicker.date
timeLabel.text = timeformat.string(from: dayFinishTimePicker.date)
}

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

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
}
}

How to pass data from one view controller to another?

How do I pass the data from an array of one view controller to another view controller?
FirstViewController:
var myArray = ["some data"]
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
let destinationVC = segue.destination as! SecondViewController
destinationVC.passedArray = myArray
}
SecondViewController:
var passedArray = [String]()
override func viewDidLoad(){
super.viewDidLoad()
print(passedArray)
}
You can try this Solution for Data Transfer.
In FirstViewController.swift
let str = "Hello It's Done"
func callsecondViewController()
{
let controller = storyboard?.instantiateViewController(withIdentifier: "second") as! secondViewController
self.present(controller, animated: true, completion: nil)
}
func temp() -> NSString
{
return str as NSString
}
in SecondViewController.swift
var data = [NSString]()
override func viewDidLoad()
{
super.viewDidLoad()
let view = ViewController()
data = [view.temp()]
print(data)
}
and you get the output String as Hello It's Done

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
}
}