0

So I am trying to create a app that is more or less a reference with static data and using a plist to store the data

I have two TableViewControllers setup on the storyboard and I wanna pass the selected cells rownumber or its data to the next view?

My initial setup:

import UIKit

var categoryNames:NSArray = []
var categorySelected:String = ""

class ListForbTVC: UITableViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    let path = NSBundle.mainBundle().pathForResource("listor", ofType: "plist")
    let dict = NSDictionary(contentsOfFile: path!)
    categoryNames = dict.objectForKey("Category") as NSArray
    println(categoryNames.count)

}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

For some reason I cannot figure out how to count the number of rows in the array?

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return 13
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell

cell.textLabel!.text = categoryNames[indexPath.row] as? String

return cell
}

What should I add here to actually pass the value through the segue? Would be neat to know how to both pass the index of the selected row and how to pass the actual value of it.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "listData" {

}

How do I recieve the information in the next view?

I have done this in obj-c and X-Code 5 but I cannot figure out how to do this in Swift.

Thanks in advance

2
  • categoryNames.count is the number of elements in the array - you are already printing it. As for passing the data, simply declare a property on your destination view controller class and set it as you do in Objective C
    – Paulw11
    Sep 28, 2014 at 11:07
  • Thanks Paul, for some stupid reason I have not gotten that statement working earlier, now it did work perfectly fine! Still however struggling with the segue :)
    – noxious
    Sep 28, 2014 at 13:17

2 Answers 2

0

You can simply declare a property in your second view controller - something like this -

class VC2 : UIViewController {

    var someString:String?

    override func viewWillAppear(animated: Bool) {
       super.viewWillAppear(animated)
       if (self.someString != nil) {
         println("Some string =\(self.someString)")
       }
    }
}

Then in your prepareForSegue, simply set it

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    if segue.identifier == "listData" {
       let destVC=segue.destinationViewController as VC2
       destVC.someString="Some value"
    } 
}
0

I have updated my answer as per swift.Please check.

    override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if (segue.identifier == "segueTest") {
    var svc = segue!.destinationViewController as secondViewController;

    svc.toPass =data}

}

Please check this http://jamesleist.com/ios-swift-passing-data-between-viewcontrollers/ Let me know if faces issue still.

1
  • I am trying to do this in swift and cannot really figure out the syntax, as far as I'm concerned what you posted is obj-c?
    – noxious
    Sep 28, 2014 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.