Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在另一个UIViewController中更新UITableView,而不在Swift中使用segue_Ios_Iphone_Uitableview_Swift - Fatal编程技术网

Ios 在另一个UIViewController中更新UITableView,而不在Swift中使用segue

Ios 在另一个UIViewController中更新UITableView,而不在Swift中使用segue,ios,iphone,uitableview,swift,Ios,Iphone,Uitableview,Swift,我对XCode比较陌生,我有一个问题。 在我的UITableViewController中,我有一个包含技巧列表的数组 var trickListArray = [tricktionaryViewController.trick(category: "", name: "", turn: "", degree: 0, description: "", instruction: "")] var learningListArray = [tricktionaryViewController

我对XCode比较陌生,我有一个问题。 在我的UITableViewController中,我有一个包含技巧列表的数组

var trickListArray = [tricktionaryViewController.trick(category: "", name: "", turn: "", degree: 0, description: "", instruction: "")]
    var learningListArray = [tricktionaryViewController.trick(category: "", name: "", turn: "", degree: 0, description: "", instruction: "")]

    var myTricksArray:[[tricktionaryViewController.trick]] {
        return [trickListArray,learningListArray]
    }
我想通过“添加到技巧列表”按钮,从另一个ViewController将类技巧的对象附加到此数组。按下此按钮后,会出现一个弹出窗口,显示“已成功添加”,但我不想切换到我的UITableViewController,只需将技巧对象附加到我的trickListArray或learningListArray,然后像这样更新我的UITableView:

@IBAction func addToTrickList(sender: AnyObject) {
        var destinationViewController: myTricksViewController = myTricksViewController(nibName: nil, bundle: nil)
        destinationViewController.trickListArray.append(self.activeTrick)
        var popUpTextString: String = "Successfully added " + String(activeTrick.name) + " to the list"
        self.popViewController.showInView(self.view, animated: true, text: popUpTextString)

    }
@IBAction func addToTrickList(sender: AnyObject) {
        var destinationViewController: myTricksViewController = myTricksViewController(nibName: nil, bundle: nil)
        destinationViewController.trickListArray.append(self.activeTrick)
        destinationViewController.myTricksTableView?.reloadData()
        var popUpTextString: String = "Successfully added " + String(activeTrick.name) + " to the list"
        self.popViewController.showInView(self.view, animated: true, text: popUpTextString)

    }
如果我在iAction中添加一个重载数据函数,如下所示:

@IBAction func addToTrickList(sender: AnyObject) {
        var destinationViewController: myTricksViewController = myTricksViewController(nibName: nil, bundle: nil)
        destinationViewController.trickListArray.append(self.activeTrick)
        var popUpTextString: String = "Successfully added " + String(activeTrick.name) + " to the list"
        self.popViewController.showInView(self.view, animated: true, text: popUpTextString)

    }
@IBAction func addToTrickList(sender: AnyObject) {
        var destinationViewController: myTricksViewController = myTricksViewController(nibName: nil, bundle: nil)
        destinationViewController.trickListArray.append(self.activeTrick)
        destinationViewController.myTricksTableView?.reloadData()
        var popUpTextString: String = "Successfully added " + String(activeTrick.name) + " to the list"
        self.popViewController.showInView(self.view, animated: true, text: popUpTextString)

    }
它不会更新UITableView。 我还没有测试阵列是否真的更新了,但应该更新


感谢您的帮助

您需要应用一些arcitecture!viewcontroller不应控制数据源。相反,将数据数组放在一个单例中,您可以从应用程序中的任何位置向该列表添加/删除对象

以下是obj-c中的架构指南:

下面是如何在swift中执行singleton类的示例:

编辑:

如果您想要一种快速简便的方法,您可以使用您的appdelegate作为单例

将数据数组作为属性放置在appdelegate中,并通过以下方式将实例获取到appdelegate:

let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let yourlist = appDelegate.listOfObjects

谢谢你的回答。与appDelegate的建议确实对我有效。你能再给我解释一下如何使用单例以及如何在其中添加属性吗?我是否创建一个新的.swift文件并声明一个新的singleton类?然后..?好吧,在尝试了你的建议之后,我设法将数据添加到我的数组中,并且能够将其打印到控制台以确保数据被添加。但我仍然无法更新另一个ViewController中的UITableView。您需要做的只是将另一个ViewController的数据数组重定向到singleton/appdelegate中的数据数组。然后在视图上显示,您只需调用reloaddata,就会看到新的数据。