
Swift 视图控制器之间的反向传值
发布日期:2021-05-27 02:41:53
浏览次数:3
分类:技术文章
本文共 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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
能坚持,总会有不一样的收获!
[***.219.124.196]2023年11月16日 08时51分33秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
ubuntu上hadoop的安装
2019-03-25
hdfs文件操作
2019-03-25
BPMN配置文件详解
2019-03-25
Activiti部署与流程定义
2019-03-25
自定义云服务开发(一)
2019-03-25
ablecloud请求uds服务
2019-03-25
IDEA打包Java Web项目
2019-03-25
IDEA远程调试tomcat
2019-03-25
vue ts开发环境之webpack配置
2019-03-25
vue ts开发环境之安装包
2019-03-25
vue ts开发环境之module.d.ts
2019-03-25
vue ts开发环境之tsconfig.json
2019-03-25
vue ts开发环境之组件
2019-03-25
spring源码解析之BeanFactory及其继承结构
2019-03-25
BeanDefinition
2019-03-25
elasticsearch-head插件显示未连接
2019-03-25
win10安装rabbitmq3.7.8
2019-03-25
AcWing 837 连通块中点的数量
2019-03-25
AcWing 240 食物链
2019-03-25
AcWing 838 堆排序
2019-03-25