简体   繁体   中英

prepareForSegue elements in Array in swift

I have two viewcontrller vc1 and vc2. In vc1 it is tableview parsed with json and it displays some names.

vc1:

var names: [String] = [] // Displays name (Ana, Mike, John ....)

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

cell.lable.text = name[indexPath.row]

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        let someName: vc2 =  segue.destinationViewController as! vc2
        someName.nameFromVC1 = //need code here

    }


vc2:

var nameFromVC1: String!

my goal is when table displays names, and I click some name say "Ana", I want "Ana" to be sent to vc2 and put in var nameFromVC1

You should use something like this in vc 1:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    // Prepare the destination view controller with the selected name
    (segue.destinationViewController as? NameDetailViewController)?
        .prepareForName(
            names[tableView.indexPathForSelectedRow!.row]
        )
}

And this in your destination view controller:

func prepareForName(name: String) {
    self.name = name
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM