在没有Segues的ViewController之间传递数据

时间:2017-08-23 00:12:21

标签: ios swift

我对快速开发很新,并且在理解如何在ViewController之间传递数据方面遇到一些问题。

我想构建一个简单的音乐播放器应用程序,它有三个视图(播放器播放列表曲目)。

开始时,播放器会显示给用户。用户可以从那里按下按钮,然后会出现播放列表视图。现在,他可以选择一个播放列表,并显示下一个视图 Tracks

如果他按下一首曲目,他会回到播放器视图,并且该曲目正在播放。所以我需要将我的曲目传递给我的PlayerViewController。

目前我正在使用Segues来显示每个ViewController。 播放器 - >播放列表 - >曲目 - >播放器

但是这会再次初始化 Player ,这意味着值/变量会被重置。我怎么能避免这个?

4 个答案:

答案 0 :(得分:1)

如果您从视图控制器B通过说present来查看控制器C,则视图控制器C可以说出控制器B作为其presentingViewController

答案 1 :(得分:0)

尝试使用 Unwind segues 传递数据,我猜他们可以帮助您。

    An unwind segue (sometimes called exit segue) can be used to navigate 
    back through push, modal or popover segues (as if you popped the navigation
     item from the navigation bar, closed the popover or dismissed the modally
     presented view controller). On top of that you can actually unwind through
     not only one but a series of push/modal/popover segues, e.g. "go back"multiple steps in your navigation hierarchy with a single unwind action.When you perform an unwind segue, you need to specify an action, which is an action method of the view controller you want to unwind to.

//ViewControllerA:

import UIKit

class ViewControllerA: UIViewController {

    var dataRecieved: String? {
        willSet {
            labelOutlet.text = newValue
        }
    }

    @IBOutlet weak var labelOutlet:UILabel!
    @IBOutlet weak var nextButtonOutlet: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    @IBAction func nextButtonAction(_ sender:UIButton) {

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateViewController(withIdentifier: "ViewControllerB") as! ViewControllerB
        controller.dataPassed = labelOutlet.text
        self.present(controller, animated: true, completion: nil)

    }

    // segue ViewControllerB -> ViewControllerA
    @IBAction func unwindToThisView(sender: UIStoryboardSegue) {
        if let sourceViewController = sender.source as? ViewControllerB {
            dataRecieved = sourceViewController.dataPassed
        }
    }

}

//ViewControllerB

import UIKit

class ViewControllerB: UIViewController , UITextFieldDelegate{

    @IBOutlet weak var textFieldOutlet: UITextField!
    var dataPassed : String?

    override func viewDidLoad() {
        super.viewDidLoad()

        textFieldOutlet.text = dataPassed

        textFieldOutlet.delegate = self
    }

    // UITextFieldDelegate
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        // User finished typing (hit return): hide the keyboard.
        textField.resignFirstResponder()
        return true
    }

    func textFieldDidEndEditing(_ textField: UITextField) {
        dataPassed = textField.text
    }

}

//从“返回”按钮单击控件并拖动以退出viewcontrollerB,如图所示。

enter image description here

答案 2 :(得分:-1)

要在View Controllers之间传递数据,请在代码中使用此块:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let feed = segue.destination as! SecondViewControllerName

    feed.variableinsecondviewcontroller = variableincurrentviewcontroller
}

如果这没有帮助,您可能需要详细说明您对代码的确切要求......

答案 3 :(得分:-1)

使用struct class并使用stuct对象访问您的对象  喜欢这个

struct SomeStruct {var name:String init(name:String){self.name = name}} var aStruct = SomeStruct(name:" Bob")var bStruct = aStruct // aStruct和bStruct is两个具有相同价值的结构! bStruct.name ="苏" println(aStruct.name)//" Bob" println(bStruct.name)//"苏"

相关问题