Swift 视图控制器之间的反向传值
发布日期:2021-05-27 02:41:53 浏览次数:21 分类:技术文章

本文共 2414 字,大约阅读时间需要 8 分钟。

ViewController.swift

import UIKitclass ViewController: UIViewController{
// Create a class variable for designation method to visit its attributes. let labelOne = UILabel(); // Create a class variable for clourse to visit. var mainContent: String = ""; @objc func mainListener(){
let svc = SecondViewController(); // Create an optional closure to get the variable. svc.closure = {
(data: String) in self.mainContent = data; self.labelOne.text = self.mainContent; print(self.mainContent); } self.present(svc, animated: true, completion: nil); } override func viewDidLoad() {
super.viewDidLoad() // Do any additional setup after loading the view. // let labelOne = UILabel(frame: CGRect(x: 30, y: 120, width: 240, height:30)); labelOne.frame = CGRect(x: 30, y: 120, width: 240, height:30); labelOne.text = "Fuyang Normal Colleage"; labelOne.textColor = UIColor.purple; labelOne.font = UIFont.boldSystemFont(ofSize: 20); self.view.addSubview(labelOne); let butttonOne = UIButton(type: UIButton.ButtonType.system); butttonOne.frame = CGRect(x: 200, y:400, width: 100, height: 30); butttonOne.setTitle("ButtonOne", for: UIControl.State()) self.view.addSubview(butttonOne); butttonOne.addTarget(self, action: #selector(mainListener), for: UIControl.Event.touchUpInside); } }

SecondViewController.swift

import UIKitclass SecondViewController: UIViewController {
// Create an optional clourse. var closure: ((String) -> Void)?; @objc func secondListener(){
// Set the content into clourse. self.closure!("Fuyang Normal University"); self.dismiss(animated: true, completion: nil); } override func viewDidLoad() {
super.viewDidLoad() self.view.backgroundColor = UIColor.orange; let butttonOne = UIButton(type: UIButton.ButtonType.system); butttonOne.frame = CGRect(x: 100, y:300, width: 120, height: 30); butttonOne.setTitle("Second", for: UIControl.State()) self.view.addSubview(butttonOne); butttonOne.addTarget(self, action: #selector(secondListener), for: UIControl.Event.touchUpInside); // Do any additional setup after loading the view. }}

转载地址:https://blog.csdn.net/kicinio/article/details/109871650 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:iOS GCD简单使用
下一篇:MySQL 常见引擎适合业务场景

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年06月28日 11时43分23秒