通过prepareForSegue传递数组的麻烦

时间:2015-02-19 07:42:47

标签: swift

我目前正在 Swift 中提高我的编程技能,在我的程序中,我试图从一个ViewController 中提取数据并将其导入我的其他视图控制器阵列。我为此使用了 prepareForSegue 。基本上,第四个vc是回收站类型的东西,其中放置了我的第二个vc中的已删除项目。这是我无法做到的部分,它没有转移。我已经添加了println来显示我的数组中的数据一旦我改回到我的第四个vc并且数组变空了。

如果有人能帮我解决这个问题,我会很高兴!感谢。

第二视图控制器(主表):

import UIKit
class SecondViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate, UITextFieldDelegate {
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var toolbar: UIToolbar!
    @IBOutlet weak var addButton: UIBarButtonItem!
    @IBOutlet weak var createButton: UIBarButtonItem!
    var projects: [String] = []
    //deleted array which is being transferred to fourthvc - check prepareForSegue method
    var deleted: [String] = []
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
        var alert = UIAlertView(title: "To edit your project", message: "Swipe left on a created project for editing options", delegate: self, cancelButtonTitle: "Cancel")
        alert.addButtonWithTitle("Continue")
        alert.show()
    }
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "FourthViewController" {
            let destination = segue.destinationViewController as FourthViewController
            destination.deletedItems = deleted
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    // Use this for swipe table view cell
    /*
    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) {
        if editingStyle == UITableViewCellEditingStyle.Delete {
            projects.removeAtIndex(indexPath.row)
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
            deleted.append(projects[indexPath.row])
        }
    } */

    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == UITableViewCellEditingStyle.Delete {
            self.deleted.append(self.projects[indexPath.row])
            projects.removeAtIndex(indexPath.row)
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) } }


    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return projects.count
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
        // Set the label text as the project name
        cell.textLabel!.text = projects[indexPath.row]
        return cell
    }
    func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
        if buttonIndex == 1 {
            if let name = alertView.textFieldAtIndex(0)?.text {
                projects.append(name)
                tableView.reloadData()
            }
        }
    }
    // Add button
    @IBAction func addButtonPressed(sender: AnyObject) {
        var alert = UIAlertView(title: "Enter the title", message: "This will be the name of your project", delegate: self, cancelButtonTitle: "Cancel")
        alert.alertViewStyle = UIAlertViewStyle.PlainTextInput
        alert.addButtonWithTitle("Continue")
        alert.show()
    }
    // Swipe
    func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]?  {
        // Delete
        var deleteButton = UITableViewRowAction(style: .Default, title: "Delete", handler: { (action, indexPath) in
            self.tableView.dataSource?.tableView?(self.tableView, commitEditingStyle: .Delete, forRowAtIndexPath: indexPath)
            println(self.projects)
            println(self.deleted)
            return
        })
        deleteButton.backgroundColor = UIColor.redColor()
    // Edit
        var editAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Edit" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
            let editMenu = UIAlertController(title: nil, message: "Edit this project", preferredStyle: .ActionSheet)
            let editAction = UIAlertAction(title: "Edit", style: UIAlertActionStyle.Default, handler: { action in
                var vc = self.storyboard?.instantiateViewControllerWithIdentifier("ThirdViewController") as ThirdViewController
                self.presentViewController(vc, animated: true, completion: nil)
                })
            let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)
            editMenu.addAction(editAction)
            editMenu.addAction(cancelAction)
            self.presentViewController(editMenu, animated: true, completion: nil)
        })
        editAction.backgroundColor = UIColor.lightGrayColor()
        return [deleteButton, editAction]
    }
}

这是我的第四个视图控制器(回收站):

import UIKit
class FourthViewController: UIViewController, UITableViewDelegate, UITextFieldDelegate, UITableViewDataSource {
    @IBOutlet weak var tableView: UITableView!
    // new array that data is being stored into from secondVC
    var deletedItems: [String] = []
    override func viewDidLoad() {
        super.viewDidLoad()
        println(deletedItems)
        // Do any additional setup after loading the view.
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == UITableViewCellEditingStyle.Delete {
            deletedItems.removeAtIndex(indexPath.row)
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) } }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return deletedItems.count
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
        // Set the label text as the project name
        cell.textLabel!.text = deletedItems[indexPath.row]
        return cell
    }
}

0 个答案:

没有答案