简体   繁体   中英

Passing data from one tableview to another tableview using segue in swift

I am really struggling to pass data from SecondViewController to ActivityFormTableViewController.

get this error: 'NSInvalidArgumentException', reason: 'Receiver () has no segue with identifier 'MajorActivity''

Code for two classes follow:

Any help would be received with gratitude.

//
//  SecondViewController.swift
//  LinkByActivity
//
//  Created by Jeremy Andrews on 2015/06/10.
// version update 23/06/2015
//  Copyright (c) 2015 Jeremy Andrews. All rights reserved.
//

import UIKit

class  SecondViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet var tableView: UITableView!

    let textCellIdentifier = "TextCell"
    var catRet = XnYCategories.mainCats("main")
    var activityDictionary = [String : [String]]()
    var key1:String!
    @IBAction func ActivityMainCats(segue:UIStoryboardSegue) {


    }

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self
    }

    // MARK:  UITextFieldDelegate Methods
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1

    }

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

        return catRet.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as! UITableViewCell
        let row = indexPath.row
        cell.textLabel?.text = catRet[row]
        return cell

    }

    // MARK:  UITableViewDelegate Methods
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        var row = indexPath.row
        let key1 = catRet[row]
        println(key1)
        performSegueWithIdentifier("MajorActivity", sender: self)
    }
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
            if (segue.identifier == "MajorActivity") {
            var svc = segue.destinationViewController as! ActivityFormTableViewController;
            svc.key2 = key1
            println(key1)

            }
        }


    }

//
//  ActivityFormTableViewController.swift
//  LinkByActivity
//
//  Created by Jeremy Andrews on 2015/07/24.
//  Copyright (c) 2015 Jeremy Andrews. All rights reserved.
//

import UIKit

class ActivityFormTableViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet var tableView2: UITableView!
    var key2:String!
    var catRet2 = XnYCategories.mainCats("Sport")

    let textCellIdentifier = "TextCell2"
    var activityDictionary = [String : [String]]()

    override func viewDidLoad() {
        super.viewDidLoad()
        println(key2)
        var catRet2 = XnYCategories.mainCats(key2)
        println(catRet2)
        tableView.delegate = self
        tableView.dataSource = self

    }

    // MARK:  UITextFieldDelegate Methods
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1

    }

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

        //var catRet2 = XnYCategories.mainCats(key2)

        return catRet2.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as! UITableViewCell
        let row = indexPath.row
    cell.textLabel?.text = catRet2[row]
        return cell

    }

    // MARK:  UITableViewDelegate Methods
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        var row = indexPath.row
        let activityKey = "CellNo_" + "famNam_" + key2
        activityDictionary = [activityKey: ["TheForm", "l1", "etc"]]
        //println(activityDictionary)

    }

}

You need to create a UINavigationController in your prepareForSegue method because it appears as that's what you have on your storyboard. Then you need to create an instance of the controller that you want the navigation controller to show and assign your properties to that.

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

        let activityFormNavController = segue.destinationViewController as! UINavigationController

        let svc = activityFormNavController.topViewController as! ActivityFormTableViewController
        svc.key2 = key1
        println(key1)

        }
    }

Storyboard Example:

在此处输入图片说明

ViewController.swift

import UIKit

class ViewController: UIViewController {

var someString : String?

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func fireSegueBttnTouched(sender: AnyObject) {

    // set my local instance variable to some value to pass

    someString = "Stack Overflow Is Great!"

    performSegueWithIdentifier("next", sender: self)
}

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

    if segue.identifier == "next" {

        // Create a UINavigationController instance first b/c that's where the segue goes

        let nextNavVc = segue.destinationViewController as! UINavigationController

        // Create an instance of the top view controller belonging to the above crated navigation controller

        let nextVc = nextNavVc.topViewController as! SecondViewController

        // Assign the instance variable from this view controller to the variable on the view controller nextVc

        nextVc.passedString = someString

    }

}

}

SecondViewController.swift

import UIKit

class SecondViewController: UIViewController {

@IBOutlet weak var outputLabel: UILabel!

var passedString : String?

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override func viewDidLayoutSubviews() {

    if passedString != nil {

        outputLabel.text = passedString!
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

Simulator Screenshots:

在此处输入图片说明

在此处输入图片说明

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