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

swift
阅读 38 收藏 0 点赞 0 评论 0

ViewController.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.
    }
    */

}


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














评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号