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)
}
Related
Okay, so I have two storyboards being incorporated into my code. One of them being the coffeeViewController, the other being the checkoutViewController. I have an array of doubles and different values incorporated into it. However, when I run the code, it shows the label that I want to edit as blank. Can someone tell me how to fix this?
This code is on my coffeeViewController
var total:[Double] = []
#IBAction func action(_ sender: Any) {
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let sum = total.reduce(0,+)
let number = "\(sum)"
let lvc = segue.destination as! checkoutViewController
lvc.am = number
}
}
This code is on my checkoutViewController
var am = String()
override func viewDidLoad()
{
super.viewDidLoad()
amount.text = am
}
I am trying to pass data to another controller with a segue after clicking on a button.
This is my code:
#IBAction func agilityDogBtnPressed(_ sender: Any) {
if let objs = controller.fetchedObjects, objs.count > 0 {
let course = objs[0]
self.performSegue(withIdentifier: "DogAgilitySegue", sender: course)
}
}
#IBAction func baseEducationBtnPressed(_ sender: Any) {
if let objs = controller.fetchedObjects, objs.count > 0 {
let course = objs[1]
self.performSegue(withIdentifier: "BaseEducationSegue", sender: course)
}
}
#IBAction func puppyBtnPressed(_ sender: Any) {
if let objs = controller.fetchedObjects, objs.count > 0 {
let course = objs[2]
self.performSegue(withIdentifier: "PuppyClassSegue", sender: course)
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "DogAgilitySegue" {
if let destination = segue.destination as? ClassDetailsVC {
if let course = sender as? Course {
destination.course = course
}
}
} else if segue.identifier == "PuppyClassSegue" {
if let destination = segue.destination as? ClassDetailsVC {
if let course = sender as? Course {
destination.course = course
}
}
} else if segue.identifier == "BaseEducationSegue" {
if let destination = segue.destination as? ClassDetailsVC {
if let course = sender as? Course {
destination.course = course
}
}
}
My problem is that the first two segues are working perfectly fine.
The third one (PuppyClassSegue) is not passing the data through.
It opens the page but the data are not showing, basically the destination.course is not happening.
Any idea how is that possible?
Thank you!
It appears that course is nil , also you can do this by hooking all buttons to the same action and setting tags for them 0,1,2 respectively
#IBAction func agilityDogBtnPressed(_ sender: UIButton) {
if let objs = controller.fetchedObjects, objs.count > 0 {
let course = objs[sender.tag]
self.performSegue(withIdentifier: "SegueFromCon", sender: course)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? ClassDetailsVC {
if let course = sender as? Course {
print("hit here ",course)
destination.course = course
}
}
}
make 1 segue named say SegueFromCon from the current vc to the detailsVC
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)
}
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.
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
}
}