I want to transfer a file or a string from a cell on the table to my viewcontroller with parse but i'm completely stuck. Here is my code for segue
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "mySegue" {
let segue: myController = segue.destinationViewController as! myController
let segue2 = tableView.indexPathForSelectedRow?.row
segue.myLabelInViewController = objects[segue2!] as! String
}
}
And here is another code for UIImage transfer
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "mySegue" {
let segue: myController = segue.destinationViewController as! myController
let segue2 = tableView.indexPathForSelectedRow?.row
segue.myUIImageView.image = UIImage(data: objects[segue2!])
}
}
I think you have a few issues going on. First, if you're using a PFQueryTableViewController, objects is an array of PFObjects. This means you will want to either pass the entire PFObject to the destination view controller or you could pass individual properties.
Second, try the following in your table view controller to pass the whole object. In the destination view controller we can set the UILabel and UIImage.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "mySegue" {
// Get the new view controller using [segue destinationViewController]
var detailScene = segue.destinationViewController as! myController
// Pass the selected object to the destination view controller
if let indexPath = self.tableView.indexPathForSelectedRow() {
let row = Int(indexPath.row)
detailScene.currentObject = (objects?[row] as! PFObject)
}
}
}
Next, add the following to the top of your detail (destination) view controller
// Container to store the view table selected object
var currentObject : PFObject?
When the detail view controller loads, take the contents of currentObject and push the values into the appropriate labels and images so that they are displayed. Update your viewDidLoad() method to look like this.
override func viewDidLoad() {
super.viewDidLoad()
// Unwrap the current object
if let object = currentObject {
myLabelInViewController.text = object["someString"] as! String
myUIImageView.image = UIImage(data: object["someImageData"] as! NSData)
}
}
I'd highly recommend you check out this great tutorial on PFQueryTableViewControllers in Swift
Related
So I'm learning Core Data and I managed to create a segue with a parent relationship that allows me to segue to show only certain items based on the category you select in the previous VC (A Category View Controller). Now I have created a separate VC after this which shows the weather based on your location and to get to it you have to click the button from the original VC (The Category View Controller) to get to the weatherViewController. However every time I try to do so and perform a segue I run into this error below :
This is the code I have so far :
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinationVC = segue.destination as! NoteTableViewController
let weatherDestinationVC = segue.destination as! WeatherViewController
if let indexLocale = noteTableView.indexPathForSelectedRow {
destinationVC.selectionCategory = noteArray[indexLocale.row]
print(indexLocale.row)
} else {
print("Error")
weatherDestinationVC.delegate = self
performSegue(withIdentifier: "goToWeather", sender: self)
}
}
#IBAction func goToWeatherView(_ sender: UIBarButtonItem) {
performSegue(withIdentifier: "goToWeather", sender: self)
}
Any help would be appreciated since I am just learning Core Data!
Conditionally cast the different segue destination view controllers or check for the segue's identifier whichever you prefer.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destinationVC = segue.destination as? NoteTableViewController {
if let indexLocale = noteTableView.indexPathForSelectedRow {
destinationVC.selectionCategory = noteArray[indexLocale.row]
print(indexLocale.row)
}
} else if let weatherDestinationVC = segue.destination as? WeatherViewController {
weatherDestinationVC.delegate = self
}
}
Also, you don't have to performSegue within prepareForSegue.
I have 4 cells and each one of them has a label inside of it.
Every time a user clicks on a cell it will take them to a different view, and in that view I have a top label that should be named like the cell label. So if I clicked on cell 001 then the next page title label should be 001. Image below to show the title label:
I use prepare for segue to pass the value of the title label, however, how can I write a code that says:
if cell label = 001 {
newViewTitle.text = cell label
}
This is my current prepare for segue code:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? VolumeImgVC {
if let VolumeNumLbl = volumEDnum as? String! {
destination.Vtitle = VolumeNumLbl
}
if let VolumeNumLbl = volumEDnum1 as? String! {
destination.Vtitle = VolumeNumLbl
}
if let VolumeNumLbl = volumEDnum2 as? String! {
destination.Vtitle = VolumeNumLbl
}
}
}
The above code does not work and only executes the last if let statement.
Any suggestions please? Thank you
create a array of cellnamelabel array on click of cell just use the tableView.indexPathForSelectedRow property to get selected indexPath and pass the data
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "detailSeque" {
if let destination = segue.destination as? VolumeImgVC {
if let indexPath = self.tableView.indexPathForSelectedRow {
let volumeNum = volumeNumLblArray[indexPath.row]
destination.Vtitle = volumeNum
}
}
}
}
Output
The Event ID is: Circle
Could not cast value of type 'ThisLondon.ViewController' (0x9c338) to 'UICollectionViewCell' (0x39dbd1a0).
(lldb)
Yes "Circle" is being output, that is what I selected, great, although I can't get that to pass over the segue to the tableView?!
here is my code:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "selectedTube" {
let detailViewController = segue.destinationViewController as! tubeDetailsTableViewController
if let indexPath = collecView.indexPathForCell(sender as! UICollectionViewCell) {
detailViewController.selectedTube = stationArray[indexPath.row]
}
}
}
I have a problem trying to pass data on to some other view controllers. I can successfully perform the segue, but the supplementary data I need doesn't make it through the segue.
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
/// Identify the row number
let rowNumber = indexPath.row
let section = indexPath.section
// Obtain the array of model names for the selected auto maker
let moviesForSelectedGenre: AnyObject? = dict_Genre_dict2[genres[section]]
dict2_Movie_MovieInfo = moviesForSelectedGenre as! Dictionary
let movieNumber:String = "\(rowNumber+1)"
movie = dict2_Movie_MovieInfo[movieNumber] as! [String]
youtubeID = movie[2]
movieTitle = movie[0]
performSegueWithIdentifier("showTrailer1", sender: self)
}
func add(sender: AnyObject) {
performSegueWithIdentifier("addMovie", sender: self)
}
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if segue == "showTrailer1"{
// Obtain the object reference of the destination view controller
let trailerViewController: MovieTrailerViewController = segue.destinationViewController as! MovieTrailerViewController
//Pass the data object to the destination view controller object
trailerViewController.youTubeMovieTrailerID = youtubeID
trailerViewController.movieTitle = movieTitle
}
else if segue == "addMovie"{
let addMovieViewController: AddMovieViewController = segue.destinationViewController as! AddMovieViewController
addMovieViewController.delegate = self
}
}
If anyone can identify why this is happening, that would be fantastic. Thanks!
You're not comparing with
segue.identifier
in
if segue == "showTrailer1"{
and
else if segue == "addMovie"{
Adding .identifier after segue will fix your problem.
In prepareForSegue you have to use segue.identifier to check which segue has been called
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if segue.identifier == "showTrailer1"{
// Obtain the object reference of the destination view controller
let trailerViewController: MovieTrailerViewController = segue.destinationViewController as! MovieTrailerViewController
//Pass the data object to the destination view controller object
trailerViewController.youTubeMovieTrailerID = youtubeID
trailerViewController.movieTitle = movieTitle
}
else if segue.identifier == "addMovie"{
let addMovieViewController: AddMovieViewController = segue.destinationViewController as! AddMovieViewController
addMovieViewController.delegate = self
}
}
Trying to pass data to an embedded table view controller. I am able to pass the data to the view controller but not the embedded table view controller. I am assuming it has something to do with child/parent relationship... just not sure on the code to use for that relationship.
Here is what I have right now. The DealDetailTableViewController is embedded in a View Controller.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "showDealDetails") {
var detailScene = segue.destinationViewController as! DealDetailTableViewController
if let indexPath = self.tableView.indexPathForSelectedRow() {
let row = Int(indexPath.row)
detailScene.dealObject = (objects?[row] as! PFObject)
}
}
First,in the contain viewController,keep a variable
weak var embedvc:DealDetailTableViewController?
Then in prepare for segue
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "showDealDetails") {
var detailScene = segue.destinationViewController as! DealDetailTableViewController
self.embedvc = detailScene;
}
}
Every time you pass data,you just need to
self.embedvc?.dealObject = ...//balabala