iOS: PrepareForSegue not working - ios

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

Related

passing data via prepareForSegue in Swift from UICollectionView to UITableView

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

Send variable value to next view controller when click a table cell

I have two table view controllers
InvoiceList view controller
InvoiceShow view controller
I use didSelectRowAtIndexPath method as bellow to get selected table cell specific value
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let rowObject = objects[indexPath.row]
let invoicehash = rowObject["hash_key"]!
}
i need to send invoicehash value to InvoiceShow controller when click the table cell of InvoiceList
i tried to use prepareForSegue function. but it is not applicable because it will trigger before the didSelectRawAtIndexPath function. so when i implemented it, gives the previous click event variable value. not correct one.
Please help me to access invoiceHash variable value from InvoiceShow controller
You will get the selected cell in prepareForSegue method itself.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let selectedIndexPath = self.tableView.indexPathForSelectedRow()!
let rowObject = objects[selectedIndexPath.row]
let invoiceHash = rowObject["hash_key"]!
let invoiceShowViewController = segue.destinationViewController as! InvoiceShowViewController
// Set invoiceHash to `InvoiceShowViewController ` here
invoiceShowViewController.invoiceHash = invoiceHash
}
You can still use a segue if you want and/or already setup on your storyboard.
You just need to connect the two view controllers in Interface Builder directly from one to another.
So, start ctrl-dragging from the controller itself and not from the TableViewCell (take a look at the screenshot)
then use the performSegueMethod with the new segue identifier like this:
self.performSegueWithIdentifier("mySegueIdentifier", sender: self)
and finally, your prepareForSegue method:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "mySegueIdentifier" {
let selectedIndex = self.invoiceTableView.indexPathForSelectedRow
//if element exist
if selectedIndex?.row < myDataSourceArray.count {
let destination = segue.destinationViewController as! InvoiceShowViewController
let invoice = myDataSourceArray[selectedIndex!.row]
destination.invoice = invoice
}
}
}
That's it!

How to transfer image with Parse TableView

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

Passing Data To An Embedded Table View (Swift)

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

how can I move to another view controller?

I have a problem with indexPathForSelectedRow()
I have 2 viewcontroller:
tableview and view controller
I want to move from tableview to another controller
and I used func prepareForSegue but I have problem in indexpathselectedrow()
This is issue for the problem:
cannot invoke "indexpathforselectedRow" with no arguments
The Xcode is a last version
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "show"
{
let indexpath = tableView.indexPathForSelectedRow()
let detailvc: fiveViewController = segue.destinationViewController as! fiveViewController
}
}
There is the indexPathForSelectedRow definition in Swift 1.2:
- (NSIndexPath *)indexPathForSelectedRow; // returns nil or index path representing section and row of selection.
There is its definition in Swift 2.0:
var indexPathForSelectedRow: NSIndexPath? { get } // returns nil or index path representing section and row of selection.
In Swift 2.0, the indexPathForSelectedRow is a property not a method, so you should change:
let indexpath = tableView.indexPathForSelectedRow()
to
let indexpath = tableView.indexPathForSelectedRow
Sounds like there is no reference to tableView or it's not of type UITableView.
override func prepareForSegue(segue: UIStoryboardSegue , sender: AnyObject?) {
if segue.identifier == "show"
{
let indexpath = self.tableView.indexPathsForSelectedRows()!
let detailv:fiveViewController = segue.destinationViewController as! fiveViewController
detailv.pic = self.menu[indexpath.row].picName
}
this link for the pic " my problem "

Resources