一、页面跳转传值2种方式:
红色界面输入值,按按钮跳转到绿色界面,并且绿色界面显示值。绿色界面按按钮跳转回红色界面。
前台界面:
》方式1:segue
1.新建两个ViewController后台代码文件RedViewController和GreenViewController
2.代码
import UIKit
class RedViewController: UIViewController {
@IBOutlet weak var messageTextField: UITextField!
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//页面跳转传值 法1:segue
let destVc:GreenViewController = segue.destination as! GreenViewController
destVc.message = messageTextField.text!
//页面跳转传值 法1:segue
}
}
import UIKit
class GreenViewController: UIViewController {
@IBOutlet weak var resultTextField: UITextField!
var message:String = ""
@IBAction func closeMe(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
resultTextField.text = message
}
}
-------------------------------------------------------------------------------------------------
》方式2:DataStore
1.新建后台文件DataStore
DataStore代码:
import UIKit
class DataStore {
var message:String = ""
//关闭外部初始化
private init() {}
//定义静态实例对象
private static var store:DataStore? = nil
//获取共享的实例对象
static func sharedStore() -> DataStore{
if store == nil {
store = DataStore()
}
return store!
}
}
2.其他代码:
import UIKit
class RedViewController: UIViewController {
@IBOutlet weak var messageTextField: UITextField!
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//页面跳转传值 法1:segue
//let destVc:GreenViewController = segue.destination as! GreenViewController
//destVc.message = messageTextField.text!
//页面跳转传值 法1:segue
//页面跳转传值 法2:DataStore
let store=DataStore.sharedStore()
store.message = messageTextField.text!
//页面跳转传值 法2:DataStore
}
}
import UIKit
class GreenViewController: UIViewController {
@IBOutlet weak var resultTextField: UITextField!
//页面跳转传值 法1:segue
//var message:String = ""
//页面跳转传值 法1:segue
@IBAction func closeMe(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
//页面跳转传值 法1:segue
//resultTextField.text = message
//页面跳转传值 法1:segue
//页面跳转传值 法2:DataStore
let store=DataStore.sharedStore()
resultTextField.text = store.message
//页面跳转传值 法2:DataStore
}
}
****************************************************************************************************
|
请发表评论