使用swift中的segue将数据从一个tableview传递到另一个tableview

时间:2015-07-26 16:42:55

标签: ios swift

我真的很难将数据从SecondViewController传递到ActivityFormTableViewController。

得到此错误:'NSInvalidArgumentException',原因:'Receiver()没有带标识符'MajorActivity'的segue''

以下两个类的代码:

任何帮助都会得到感谢。

//
//  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)

    }

}

1 个答案:

答案 0 :(得分:0)

您需要在prepareForSegue方法中创建一个UINavigationController,因为它显示为您在故事板上的内容。然后,您需要创建希望导航控制器显示的控制器实例,并将属性分配给该实例。

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)

        }
    }

故事板示例:

enter image description here

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

}

模拟器截图:

enter image description here

enter image description here