找到你要的答案

Q:Passing data between 2 view controllers in Swift

Q:在SWIFT 2视图控制器之间传递数据

I have 2 view controllers, created separately. It's not navigation controller or other. I've 2 files for each of them: ViewController.swift, SecondViewController.swift. Second VC is called audioList

In 1-st VC I have a button and by clicking on it I'm opening second VC using code

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)

})

There is an array called music in SecondViewController. I want to fill it with elements before code which you can see upper - from 1-st VC.

I've read this and that one post, others, read about doing it by creating protocol with function which changes data, tried a lot with segues, their performing and prepareFor-functions - but nothing worked.

Help me please with filling array in 2-nd VC from button-click-action in 1-st VC

Update:

In SecondViewController.swift I have this code:

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
}

我有2个视图控制器,分别创建。这不是导航控制器或其他。我给每个人2个文件:viewcontroller.swift,secondviewcontroller.swift。二VC称为audiolist

在1 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数组称为音乐。我想用元素在代码中可以看到从VC上1。

我读这一后,别人读通过创建与功能变化的数据协议做了,试了很多就,他们的表演和准备功能,但没有工作。

请帮我填在1 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
}
answer1: 回答1:

Create and fill an array on your first view controller. Then declare an array of the same structure on your second view controller. Then fill it in the segue.

VC1:

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

VC2:

    import UIKit

        class audioList: UIViewController {
        var music2 : [String] 

        viewDidLoad()
        {

        }
}

Create and fill an array on your first view controller. Then declare an array of the same structure on your second view controller. Then fill it in the segue.

VC1:

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

2:

    import UIKit

        class audioList: UIViewController {
        var music2 : [String] 

        viewDidLoad()
        {

        }
}
answer2: 回答2:
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)
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)
swift  viewcontroller  between