swift - 通过 prepareforsegue 在 2 个视图控制器之间添加值 [英] swift - adding values between 2 view controllers by prepareforsegue

查看:22
本文介绍了swift - 通过 prepareforsegue 在 2 个视图控制器之间添加值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个 ViewController:ViewController1、ViewController2.ViewController1 中的添加"按钮将调出 ViewController2.然后,用户将在 ViewController2 中输入值,然后prepareforsegue 将这些值传回 ViewController1.然后将这些值附加到 viewcontroller1 中类型数组的属性上.

I have 2 ViewControllers: ViewController1, ViewController2. An "Add" button in ViewController1 will bring up ViewController2. User will then enter in values into ViewController2 which will then be passed back into ViewController1 by prepareforsegue. These values are then append onto the properties of type array in viewcontroller1.

这是重复的,用户继续按下添加按钮从 ViewController1 中调出 ViewController2,然后添加新值以通过 prepareforsegue 附加到 ViewController1 中的数组类型的属性上.

This is being repeated, whereby the user continue pressing the add button to bring up ViewController2 from ViewController1, and then add in new values to be appended onto the properties of array type in ViewController1 via prepareforsegue.

然而,在从 ViewController2 附加第一组值之后,情况并非如此.第二组值将覆盖第一组值.

However this is not the case after the first set of values being appended from ViewController2. The second set of values will overwrite the first set of values.

示例.

第一次通过后->力=[1],刚度[1]

After the first passed -> force =[1], stiffness[1]

第二次通过后->力=[2],刚度[2]

After the second passed -> force=[2], stiffness[2]

我想要这个 -> 力 = [1,2],刚度 [1,2]

I will want this -> force = [1,2], stiffness[1,2]

我想继续添加直到 -> force = [1,2,3,4,5],刚度 [1,2,3,4,5]

I want to continue adding until -> force = [1,2,3,4,5], stiffness[1,2,3,4,5]

ViewController1

class ViewController1: UITableViewController, UITableViewDataSource {
    var force = [Float]()
    var stiffness = [Float] ()
@IBAction func Add(sender: AnyObject) { }

}

ViewController2

class ViewController2: UIViewController {
        var forceVar : Float = 0.0
        var stiffVar : Float = 0.0

@IBAction func submit(sender: AnyObject) {
        self.performSegueWithIdentifier("springSubmit", sender: sender)
        }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
        if(segue.identifier == "springSubmit") {
            var svcSubmitVariables = segue.destinationViewController as ViewController1

            svcSubmitVariables.force.append(forceVar)
            svcSubmitVariables.stiffness.append(stiffVar)   
        }

推荐答案

performSegueWithIdentifier 方法将始终初始化新的视图控制器,因此在您的示例视图控制器中显示 ViewController2 与提交数据后初始化的对象不同,并且这个新初始化的对象将只有初始化值.为了实现您的需求,您需要在 ViewController1 上声明一个函数,该函数会将数据附加到您的数组中,将该函数传递给 ViewController2 并在提交方法上调用它,以便您的视图控制器看起来像这样:

The performSegueWithIdentifier method will always initialize new view controller so in your example view controller that showed ViewController2 is different object from object initialized after submitting data and ofc this newly initialized object will have only initialize values. To achieve what you are looking for you will need to declare a function on your ViewController1 that will append data to your arrays, pass that function to ViewController2 and call it on submit method so your view controllers would look similar like these:

ViewController1

class ViewController1: UITableViewController, UITableViewDataSource {

    var force = [Float]()
    var stiffness = [Float] ()

    override func viewWillAppear(animated: Bool) {

     //check if values are updated
     println(force)
     println(stiffness)
    }

    @IBAction func Add(sender: AnyObject) { 
        self.performSegueWithIdentifier("viewController2", sender: sender)
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
        if(segue.identifier == "viewController2") {

            var addVariables = segue.destinationViewController as ViewController2
            addVariables.submitFunc = appendData
        }          
    }

    func appendData(newForce:Float,newStiffness:Force){
        force.append(newForce)
        stiffness.append(newStiffness)
    }       
}

ViewController2

class ViewController2: UIViewController {
    var forceVar : Float = 0.0
    var stiffVar : Float = 0.0
    var submitFunc:((newForce:Float,newStiff:Float)->())!

    @IBAction func submit(sender: AnyObject) {

        submitFunc(forceVar,stiffVar)
        self.dismissViewControllerAnimated(false, completion: nil)

    }
}

这篇关于swift - 通过 prepareforsegue 在 2 个视图控制器之间添加值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆