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
}
}
}
}
Related
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
}
}
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
Hello StackOverflow,
I'm just picking up swift and trying to implement data being passed between UITableView Cell to a UIViewController which will show a detailed view of the info shown on the tableview, and whenever I test the application on my emulator first time I press a table cell it passes an empty string and then when I try pressing another cell the viewController shows the string that was supposed to be seen earlier.I pasted the code I have for my tableview didSelectRowAtIndexPath below.
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: false)
var7 = lat[indexPath.item]
var6 = long[indexPath.item]
var5 = items[indexPath.item]
var1 = detail[indexPath.item]
var2 = date[indexPath.item]
var3 = wop[indexPath.item]
var4 = ViewController()
nextView.locationPassed = var1
//self.performSegueWithIdentifier("DetailPush", sender: self)
println("value stored in var1: \(var1)")
//println("The selected indexPath is \(indexPath.item + 1)")
println("The stored id is: \(storeSend)")
}
Here is my implementation for my push segue method
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "DetailPush"
{
if let crimesView = segue.destinationViewController as? ViewController {
crimesView.locationPassed = var1
//println("The passing address is: \(var1)")
}
}
}
Any idea on why I'm getting data delayed during the segue?
Thank you
Solution Found: I edited my prepareForSegue method with the following and it fixed my issue
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
//Adding the indexPath variable for the selected table Row within the segue
var indexPath: NSIndexPath = self.tableView.indexPathForSelectedRow()!
if segue.identifier == "DetailPush"
{
if let crimesView = segue.destinationViewController as? ViewController {
//Then just pass the data corresponding to the array I created identified by the index of the selected row
crimesView.locationPassed = self.arrayName[indexPath.row]
println("The passing address is: \(self.addressSend)")
}
}
}
I found the solution by watching some online videos and all I did to fix my issue was redefine my prepareForSegue function with:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
//Adding the indexPath variable for the selected table Row within the segue
var indexPath: NSIndexPath = self.tableView.indexPathForSelectedRow()!
if segue.identifier == "DetailPush"
{
if let crimesView = segue.destinationViewController as? ViewController {
//Then just pass the data corresponding to the array I created identified by the index of the selected row
crimesView.locationPassed = self.arrayName[indexPath.row]
println("The passing address is: \(self.addressSend)")
}
}
}
And it seems to work like a regular segue for me.......Thank you for all the suggestions given me
you said you are doing the prepareForSegue from async request
so try this:
if segue.identifier == "DetailPush"
{
dispatch_async(dispatch_get_main_queue()) {
if let crimesView = segue.destinationViewController as? ViewController {
crimesView.locationPassed = var1
//println("The passing address is: \(var1)")
}
}
}
try to remove the line
tableView.deselectRowAtIndexPath(indexPath, animated: false)
see if it still happens.
maybe move it to the end
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 "