Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.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
在Swift中的两个视图控制器之间传递数据_Swift_Viewcontroller_Between - Fatal编程技术网

在Swift中的两个视图控制器之间传递数据

在Swift中的两个视图控制器之间传递数据,swift,viewcontroller,between,Swift,Viewcontroller,Between,我有两个视图控制器,分别创建。它不是导航控制器或其他。我有两个文件,分别是:ViewController.swift,SecondViewController.swift。第二个VC称为音频列表 在第一个VC中,我有一个按钮,点击它,我用代码打开第二个VC dispatch_sync(dispatch_get_main_queue(), { () -> Void in let storyboard = UIStoryboard(name: "Main", bundle: nil)

我有两个视图控制器,分别创建。它不是导航控制器或其他。我有两个文件,分别是:
ViewController.swift
SecondViewController.swift
。第二个VC称为音频列表

在第一个VC中,我有一个按钮,点击它,我用代码打开第二个VC

dispatch_sync(dispatch_get_main_queue(), { () -> Void in

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let controller = storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController
    self.presentViewController(controller, animated: true, completion: nil)

})
SecondViewController中有一个名为
music
的数组。我想在代码之前用元素填充它,你可以从1-st VC中看到

我读过一篇文章,其他的,读过关于通过创建协议来改变数据的文章,尝试了很多segues,它们的执行和函数的准备,但是没有任何效果

请帮助我在第二个VC中填充
数组
按钮单击第一个VC中的操作

更新

SecondViewController.swift
中,我有以下代码:

var music: [String] = ["test"]

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return music.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "mycell")
    cell.textLabel!.text = music[indexPath.row]

    return cell
}

在第一个视图控制器上创建并填充阵列。 然后在第二个视图控制器上声明相同结构的数组。然后把它填进这段

VC1:

VC2:


这很可惜,但也不管用。我用代码表单2-NDVC更新了我的帖子,可能是将数据打印到tableView时出错了。但在2-VC打开后,我看到只有“测试”可以上传你的项目吗?
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController
controller.music = ["my", "data"]
self.presentViewController(controller, animated: true, completion: nil)
    var music : [String] = ["Song1", "song2", "Song3"]

    dispatch_sync(dispatch_get_main_queue(), { () -> Void in

                            let storyboard = UIStoryboard(name: "Main", bundle: nil)
                            let controller = storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController

                            controller.music2 = self.music

                            self.presentViewController(controller, animated: true, completion: nil)
                        })
    import UIKit

        class audioList: UIViewController {
        var music2 : [String] 

        viewDidLoad()
        {

        }
}