iOS開發(swift):頁面跳轉傳值(續)
阿新 • • 發佈:2018-11-13
副標題:.xib檔案的介面與.storyboard的介面相互跳轉
一、.storyboard檔案的介面跳轉到.xib檔案的介面
0.回顧:沿用上一篇文章裡.storyboard的介面。現在要實現點選綠色介面(.storyboard)按鈕跳轉至新的藍色介面(.xib)。
1.下面新建藍色介面檔案(.xib)
勾選xib
建好後會產生兩個檔案
前臺介面(BlueViewController.xib)
後臺程式碼:
import UIKit class BlueViewController: UIViewController { override init(nibName nibNameOrNil: String?,bundle nibBundleOrNil: Bundle?) { super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } }
2.修改綠色介面(.storyboard)檔案程式碼
import UIKit class GreenViewController: UIViewController { @IBOutlet weak var resultTextField: UITextField! //頁面跳轉傳值 法1:segue //var message:String = "" //頁面跳轉傳值 法1:segue @IBAction func closeMe(_ sender: Any) { //頁面跳轉傳值 法1、2:用於關閉綠色頁面,回到紅色頁面 //self.dismiss(animated: true, completion: nil) //頁面跳轉傳值 法1、2:用於關閉綠色頁面,回到紅色頁面 let vc:BlueViewController=BlueViewController(nibName:"BlueViewController",bundle:nil) self.present(vc, 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 } }
*******************************************************************************************************
二、.xib檔案的介面跳轉到.storyboard檔案的介面
1..storyboard檔案中新拖入一個介面,背景色改為黃色
右側屬性欄Storyboard ID設為“yellowView”
2.在藍色介面(.xib)上新增按鈕
藍色介面(.xib)後臺程式碼:
import UIKit class BlueViewController: UIViewController { //綠色介面跳轉至藍色介面程式碼 override init(nibName nibNameOrNil: String?,bundle nibBundleOrNil: Bundle?) { super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } //新增:藍色介面跳轉至黃色介面程式碼 @IBAction func gotoYellow(_ sender: Any) { let storyboard = UIStoryboard(name:"Main",bundle:nil) let vc = storyboard.instantiateViewController(withIdentifier: "yellowView") self.present(vc, animated: true, completion: nil) } }
*************************************************************************************************