swift 将数据从一个视图控制器传输到另一个视图控制器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift 将数据从一个视图控制器传输到另一个视图控制器相关的知识,希望对你有一定的参考价值。

//
//  ViewController.swift
//  Segues
//
//  Created by John on 27/03/2018.
//  Copyright © 2018 Rom and Ram. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var textField: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func buttonPressed(_ sender: Any) {
        
        
        performSegue(withIdentifier: "goToSecondScreen", sender: self)
        
    }
    
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "goToSecondScreen" {
            
            let destinationVC = segue.destination as! SecondViewController
            
            destinationVC.textPassedOver = textField.text!
        }
    
    
    }
    
    
}



---------------------

SecondviewController.swift

//
//  SecondViewController.swift
//  Segues
//
//  Created by John on 27/03/2018.
//  Copyright © 2018 Rom and Ram. All rights reserved.
//

import UIKit

class SecondViewController: UIViewController {

    
    var textPassedOver : String?
    
    
    
    @IBOutlet weak var label: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()

        
        label.text = textPassedOver
        
        
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}


---------------------------------














以上是关于swift 将数据从一个视图控制器传输到另一个视图控制器的主要内容,如果未能解决你的问题,请参考以下文章

Swift 4:将数据从集合视图单元传递到另一个没有故事板的视图控制器

如何使用 SWrevaelviewController swift 3 将数据从 tableview 的特定行传递到另一个视图控制器

如何将数据从表视图传递到另一个视图控制器

在swift 3中将json id从一个表视图控制器传递到另一个表视图控制器

使用 swift 将 y contentOffset 从 scrollViewDidScroll(scrollView 传递到另一个视图控制器

Swift:将数据从视图控制器传递到导航控制器