Pass image from collectionview to VC - swift

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.

Related

Swift Send data from Popup to Another View

I'm trying to send data from popup view to DataView.
it actually works ! .However, when I go back to popup view to edit the text it doesn't show the text that was entered and sent to DataView.
I'm sending the data through protocol.
PopupView
protocol DataEnteredDelegate: class {
func userDidEnterInformation(data: String)}
#IBAction func DoneButton(_ sender: Any) {
if let data = openTextView.text {
delegate?.userDidEnterInformation(data: data)
self.dismiss(animated: true, completion: nil)
}
}
DataView
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "openSummary" {
let sendingVC: popupViewController = segue.destination as! popupViewController
sendingVC.delegate = self
}
}
// Protocol receving data from popup
func userDidEnterInformation(data: String) {
addJobSum.text = data
}
After the PopupViewController gets dismissed, it gets destroyed and no new instances are able to know what an old instance had as a text value for the Text View.
To fix it - create a string property inside PopupViewController and initialize the Text View's text property with its value inside some method, such as viewDidLoad():
class PopupViewController: UIViewController {
var textData: String?
/* more code */
func viewDidLoad() {
/* more code */
if let data = textData {
openTextView.text = data
}
}
}
Then, you'll have to inject the proper / needed text for textData inside prepare(for:) method (right before it's presented):
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "openSummary" {
let sendingVC = segue.destination as! PopupViewController
sendingVC.delegate = self
// This is the new line to be added:
sendingVC.textData = addJobSum.text
}
}

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

pass NSArray to next viewcontroller in 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)
}

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

show details of filtered array Xcode 6

i made UIsearch bar to search in my tableview and everything is ok, but the problem when i want to pass data from filtered array to another controller ,after i made the search,
here my prepare
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "ahmed" {
if let indexPath = self.tableView.indexPathForSelectedRow() {
print(indexPath.row)
var object = Persons[indexPath.row]
(segue.destinationViewController as DetailViewController).name = object.name
}
}
}
this code work for non searched data which code should i write to pass searched data
i add this code and that works
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "ahmed" {
if((searchDisplayController?.active) != nil)
{
if let indexpath=searchDisplayController?.searchResultsTableView.indexPathForSelectedRow()
{
var object=filtered[indexpath.row]
(segue.destinationViewController as DetailViewController).name = object.name
}
}
if let indexPath = self.tableView.indexPathForSelectedRow() {
print(indexPath.row)
var object = Persons[indexPath.row]
(segue.destinationViewController as DetailViewController).name = object.name
}
}
}
Declare the object at Destination view controller and access directly. check below code.
//Destination View controller
import UIKit
var marCategoryList : NSMutableArray!
class tsCategoryViewController: UIViewController {
// Source view controller
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
var objTest = tsCategoryViewController()
let marImageNam = NSMutableArray(objects:"1.jpeg","2.jpeg","3.jpeg","4.jpeg","5.jpeg","6.jpeg","7.jpeg","8.jpeg","9.jpeg","10.jpeg")
marCategoryList = marImageNam
println("\(marCategoryList)")
}