在swift中通过segue传递数组

时间:2015-10-12 21:15:30

标签: ios arrays swift segue

我使用Swift将数组从SecondViewController传递到我的FirstViewController,我已经挣扎了好几天了。

根据我的研究,我发现segueprepareForSegue是可行的选择,但我似乎无法弄明白。我做错了什么?

我在SecondViewController中的prepareForSegue看起来像这样:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        var DVC: FirstViewController = segue.destinationViewController as! FirstViewController
        DVC.mySeguedArray = myIncomeArray
}

我的FirstViewController看起来像这样:

class FirstViewController: UIViewController {
@IBOutlet weak var textView: UITextView!
var myIncomeArray: [Income] = []
var mySeguedArray: [Income] = []

override func viewDidLoad() {
    super.viewDidLoad()
    myIncomeArray = mySeguedArray
}

另外值得一提的是,我确定我在SecondViewController填充数组,因为我在添加新{{}时打印出数组的大小来测试它。 1}}对象。但Income中的数组在FirstViewController之后检查大小时,只返回0

编辑:添加了故事板的图片 StoryBoard

3 个答案:

答案 0 :(得分:1)

您知道Swift中的数组是值类型吗?因此,当您将数组分配给其他变量或将其传递给某个方法时,将传递该值;不是对实际对象的引用。将数组传递给第二个视图控制器时,它会复制原始数组的内容。然后,您将添加到此数组,该数组不会更改原始数组的内容。

我建议你从第二个视图控制器创建某种回调或委托模式到第一个视图控制器来通知更改,这样第一个视图控制器也会修改原始数组内容。

这里有一小部分代码可以帮助您理解这一点,

let originalArray = ["a", "b", "c"]

var newArray = originalArray

newArray.append("d")

print(originalArray) // prints ["a", "b", "c"]
print(newArray) // prints ["a", "b", "c", "d"]

答案 1 :(得分:1)

您可以使用协议在视图控制器之间传递数据

protocol YourFirstViewControllerDelegate: class {
    func onButtonClicked(yourString: [String])
}

当您想要显示第一个视图控制器时,用于在主视图控制器中将数据从第一个视图控制器传递到第二个控制器:

  1. 您应该将主视图控制器设置为视图控制器委托,yourFirstViewController.setDlegate(self)

  2. 在FirstViewController中,您应该使用onButtonClicked([String])

  3. 将事件引发到main
  4. 在onButtonClicked([String])中,您可以调用secondViewController并使用onSecondViewController([String])传递数组

    class MainViewController: UIViewController,YourFirstViewControllerDelegate{
    var yourString = [String]()
    weak var vcFirst: FirstViewController?
    weak var vcSecond: SecondViewController?
    func onFirstViewController(){
    
        if (self.vcFirst == nil){
            self.vcFirst = self.storyboard?.instantiateViewController(withIdentifier: "FirstViewController") as? FirstViewController
            self.vcFirst!.view.translatesAutoresizingMaskIntoConstraints = false
            self.vcFirst?.setDelegate(youeFirstViewControllerDelegate: self)
        }
        self.setActiveController(viewController: self.vcFirst)
    
    }
    
    func onSecondViewController(yourString:[String]){
        if (self.Second == nil){
            self.Second = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController
            self.vcSecond!.view.translatesAutoresizingMaskIntoConstraints = false
    self.vcSecond.setArray(yourArray: self.yourArray)
        }
        self.setActiveController(viewController: self.vcSecond)
    
    }
    
    ....// your view controller method
    
    func onButtonClicked(yourString: [String]){
    self.yourString = yourString
    onSecondViewController(yourString:self.yourString)
    }
    
  5. 在您的第一个视图控制器

    class FirstViewController: UIViewController{
        weak var yourFirstViewControllerDelegate: YourFirstViewControllerDelegate?
        var yourString = ["test1","test2"]
    //  all your overide method should be here
        func setDelegate(yourFirstViewControllerDelegate:  YourFirstViewControllerDelegate){ 
        self.yourFirstViewControllerDelegate = yourFirstViewControllerDelegate
      }
    
    func onButtonClicked(){
         self.yourFirstViewControllerDelegate.onButtonClicked(self.yourString)
        }
    

    在您的第二个视图控制器中

    class SecondViewController: UIViewController{
    var yourString = [String]
    //  all your overide method should be here and you can use your [String]
     func setArray(yourString:[String]){
     self.yourString = yourString
     }
    }
    

    我现在没有Mac OS来检查我的代码,请阅读此代码而不是复制,我明天将编辑此代码

答案 2 :(得分:0)

您的代码看起来很好,使用以下的prepareforsegue,因为它有助于找出问题所在

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "segueIdInStoryboard" {
        if let DVC = segue.destinationViewController as? FirstViewController{
            DVC.mySeguedArray = myIncomeArray
        } else {
            print("Data NOT Passed! destination vc is not set to firstVC")
        }
    } else { print("Id doesnt match with Storyboard segue Id") }
}

class FirstViewController: UIViewController 
{
     @IBOutlet weak var textView: UITextView!
     var myIncomeArray: [Income]!
     var mySeguedArray: [Income]!{
         didSet{
             myIncomeArray = mySeguedArray //no need to call viewDidLoad
         }
     }
}