បន្ទាបពីយើងសិក្សារបៀបបោះទិន្ន័យតាមរយៈ Seque មកយើងមកសិក្សាបោះទិន្ន័យតាម Storyboard Id ម្តង។
ការបោះទិន្ន័យតាមរយៈ Storyboard Id វាស្រដឹងទៅនឹង Seque ដែរ
យើងបានបង្កើត ViewController និង Class ដូចទៅនឹងមេរៀន Seque ដែរ
Screen ទី២ យើងត្រូវដាក់ឈ្មោះឲ្យ Storyboard ID វា
width="100%">
នៅក្នុង ViewController សរសេរកូដដូចទៅនឹងមេរៀន Seque ដែរ
import UIKit
class ScreenTwoViewController: UIViewController {
@IBOutlet weak var label: UILabel!
var text: String?
override func viewDidLoad() {
super.viewDidLoad()
if let text = text {
label.text = text
}
}
}
សិក្សា screen ទី១
នៅ screen ទី១ នេះត្រូវអូស UIButton ចូលទៅ ViewCotroller
យើងសរសេរកូននៅក្នុង UIButton ដោយបង្កើត Varaible មួយគឺ៖
@IBAction func button(_ sender: UIButton) {
let screenTwoVC =
}
ដោយយើងបោះតាម storyboard ហៅវា
let screenTwoVC = storyboard
បន្ទាបមកយើង Instantitiate
let screenTwoVC = storyboard?.instantiateViewController(withIdentifier: String)
withIdentifier: ដាក់ឈ្មោះ storyboard Id ដែលយើងបានដាក់ពីខាងលើមក
let screenTwoVC = storyboard?.instantiateViewController(withIdentifier: "showScreen2")
បន្ទាបមកយើង cast ទៅ ViewController នៃ screen ទ២ របស់យើង
let screenTwoVC = storyboard?.instantiateViewController(withIdentifier: "showScreen2") as! ScreenTwoViewController
អាចបោះទិន្ន័យពី TextField ទៅ Label នៃ screen ទី២ របស់យើងបានហើយ
let screenTwoVC = storyboard?.instantiateViewController(withIdentifier: "showScreen2") as! ScreenTwoViewController
screenTwoVC.text = textField.text
យើងសរសរកូដដើម្បីបើក screen ទី២
navigationController?.pushViewController(viewController: UIViewController, animated: Bool)
navigationController នេះដោយសារយើងបាន Embed in ពីដំបូងទើបវាអាចហៅមកប្រើបាន
navigationController?.pushViewController(screenTwoVC, animated: true)
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func button(_ sender: UIButton) {
let screenTwoVC = storyboard?.instantiateViewController(withIdentifier: "showScreen2") as! ScreenTwoViewController
screenTwoVC.text = textField.text
navigationController?.pushViewController(screenTwoVC, animated: true)
}
}
0 Comments