1.如果我们使用Swift语言进行传值…例如:填写文本框内容进行反向传值展示出来
(1)创建TableVC继承UITableViewController
(2)创建一个要跳转到的视图OneViewController继承与 UIViewController
下面我们要在AppDelegate 写一个导航!!!记住写的是TableVC导航可不是ViewController 如果写错不出现导航
let vc = TableVC()
let nac=UINavigationController(rootViewController:vc)
self.window?.rootViewController=nac
=========
在TableVC中
首先创建一个空分区
var sectionName:[String]=[]
var rowName1:[String]=[]
var rowName2:[String]=[]
var rowName3:[String]=[]
var rowName4:[String]=[]
var cellNum:Int=0
写导航内容.左边按钮.给分区内容.创建view在表格页眉
override func viewDidLoad() {
super.viewDidLoad()
//导航文字
self.navigationItem. =\"我是世界上最帅的男人\"
//给分区写内容
sectionName=[\"小王\",\"小张\",\"小李\",\"小马\"]
self.tableView.delegate=self;
self.tableView.dataSource=self;
//创建视图 将视图写在表格页眉
self.tableView.tableFooterView=UIView()
//左边按钮
self.navigationItem.leftBarButtonItem=UIBarButtonItem( : \"全部\", style: .plain, target: self, action: #selector(whole))
}
//点击左边按钮
@objc func whole(){
if rowName1.count>0{
print(\"小明:\\(rowName1[0]),\\(rowName1[1]),\\(rowName1[2])\")
}
if rowName2.count>0{
print(\"小红:\\(rowName2[0]),\\(rowName2[1]),\\(rowName2[2])\")
}
if rowName3.count>0{
print(\"张三:\\(rowName3[0]),\\(rowName3[1]),\\(rowName3[2])\")
}
if rowName4.count>0{
print(\"李四:\\(rowName4[0]),\\(rowName4[1]),\\(rowName4[2])\")
}
}
分区数
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return sectionName.count
}
根据分区数判断每一个分区给多少表格个数
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//每一个分区的表格对应对象的个数
switch section {
case 0:
return rowName1.count
case 1:
return rowName2.count
case 2:
return rowName3.count
case 3:
return rowName4.count
default:
return 0
}
}
重用机构 并给每个分区里面的表格赋值
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell:UITableViewCell! = tableView.dequeueReusableCell(withIdentifier: \"cell\")
if cell==nil{
cell = UITableViewCell(style: .default, reuseIdentifier: \"cell\")
}
switch indexPath.section {
case 0:
cell.textLabel?.text = rowName1[indexPath.row]
case 1:
cell.textLabel?.text = rowName2[indexPath.row]
case 2:
cell.textLabel?.text = rowName3[indexPath.row]
case 3:
cell.textLabel?.text = rowName4[indexPath.row]
default:
\"\"
}
return cell
}
页眉高度
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 80
}
在页眉中创建一个新的View,里面写按钮 .和label
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
//创建view
let view=UIView( : CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50))
//view背景颜色
view.backgroundColor=UIColor.lightGray
//创建button
let btn = UIButton( : CGRect(x:UIScreen.main.bounds.width-80-10, y: 10, width: 80, height: 30))
btn.set (\"录入分数\", for: .normal)
btn.addTarget(self, action: #selector(buton), for: .touchUpInside)
btn.set Color(UIColor.red, for: .normal)
view.addSubview(btn)
btn.tag = 100+section
let label = UILabel( : CGRect(x: 10, y: 10, width: 60, height: 30))
view.addSubview(label)
label.text = sectionName[section]
let lineView = UIView( : CGRect(x: 0, y: 49, width: UIScreen.main.bounds.width, height: 1))
view.addSubview(lineView)
lineView.backgroundColor = UIColor.black
return view
}
//设置页眉高度
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
初始化要跳转的视图 …调用OneViewController中的block==vc.jumpScore.根据按钮tag值进行传值
@objc func buton(btn:UIButton){
print(btn.tag)
//初始化要跳转的页面
let vc = OneViewController()
///结构体
vc.jumpScore = {
(str1,str2,str3)->() in
switch btn.tag {
case 100:
self.rowName1.removeAll()
self.rowName1.append(\"语文:\\(str1)\")
self.rowName1.append(\"数学:\\(str2)\")
self.rowName1.append(\"英语:\\(str3)\")
case 101:
self.rowName2.removeAll()
self.rowName2.append(\"语文:\\(str1)\")
self.rowName2.append(\"数学:\\(str2)\")
self.rowName2.append(\"英语:\\(str3)\")
case 102:
self.rowName3.removeAll()
self.rowName3.append(\"语文:\\(str1)\")
self.rowName3.append(\"数学:\\(str2)\")
self.rowName3.append(\"英语:\\(str3)\")
case 103:
self.rowName3.removeAll()
self.rowName3.append(\"语文:\\(str1)\")
self.rowName3.append(\"数学:\\(str2)\")
self.rowName3.append(\"英语:\\(str3)\")
default:
\"\"
}
self.tableView.reloadData()
}
self.navigationController?.pushViewController(vc, animated: true)
}
OneViewcontroller视图中
创建结构体typealias blockScore = (String,String,String)->()
因为我们写的成绩是字符串
创建一个block 便于调用 var jumpScore:blockScore?
//结构体
typealias blockScore = (String,String,String)->()
var jumpScore:blockScore?
var tf1:UITextField!
var tf2:UITextField!
var tf3:UITextField!
创建文本框 和按钮
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.navigationItem. = \"分数录入\"
view.backgroundColor = UIColor.white
let lab1 = UILabel( : CGRect(x: 30, y: 200, width:60 , height: 60))
view.addSubview(lab1)
lab1.text = \"语文\"
let lab2 = UILabel( : CGRect(x: 30, y: 200+lab1. .height+10, width:60 , height: 60))
view.addSubview(lab2)
lab2.text = \"数学\"
let lab3 = UILabel( : CGRect(x: 30, y: 200+lab1. .height+10+lab2. .height+10, width:60 , height: 60))
view.addSubview(lab3)
lab3.text = \"英语\"
tf1 = UITextField( : CGRect(x: 50+lab1. .width+5, y: 200, width: UIScreen.main.bounds.width-(50+lab1. .width+5)-10, height: 60))
view.addSubview(tf1)
tf1.placeholder = \"请输入分数\"
tf1.borderStyle = .roundedRect
tf2 = UITextField( : CGRect(x: 50+lab1. .width+5, y: 200+tf1. .height+10, width: UIScreen.main.bounds.width-(50+lab1. .width+5)-10, height: 60))
view.addSubview(tf2)
tf2.placeholder = \"请输入分数\"
tf2.borderStyle = .roundedRect
tf3 = UITextField( : CGRect(x: 50+lab1. .width+5, y: 200+tf1. .height+10+tf2. .height+10, width: UIScreen.main.bounds.width-(50+lab1. .width+5)-10, height: 60))
view.addSubview(tf3)
tf3.placeholder = \"请输入分数\"
tf3.borderStyle = .roundedRect
let btn1 = UIButton( : CGRect(x: (UIScreen.main.bounds.width-100)/2-60, y: 200+lab1. .height+10+lab2. .height+10+lab3. .height+30, width: 100, height: 80))
view.addSubview(btn1)
btn1.set (\"保存\", for: .normal)
btn1.addTarget(self, action: #selector(save), for: .touchUpInside)
btn1.set Color(UIColor.red, for: .normal)
let btn2 = UIButton( : CGRect(x: (UIScreen.main.bounds.width-100)/2+60, y: 200+lab1. .height+10+lab2. .height+10+lab3. .height+30, width: 100, height: 80))
view.addSubview(btn2)
btn2.set (\"返回\", for: .normal)
btn2.addTarget(self, action: #selector(back), for: .touchUpInside)
btn2.set Color(UIColor.red, for: .normal)
}
点击保存按钮的时候…我们将输入的值返回给block
@objc func save() {
//将文本内容返回给jumpScore中 便于传值
self.jumpScore!(tf1.text!,tf2.text!,tf3.text!)
self.navigationController?.popViewController(animated: true)
}
点击返回按钮进行页面返回
@objc func back() {
self.navigationController?.popViewController(animated: true)
}
继续阅读与本文标签相同的文章
下一篇 :
Hello 湖南,5G真的来了
-
曾经风靡一时的360安全卫士为什么如今没落了?
2026-05-18栏目: 教程
-
用C语言编程,如何节省存储空间?
2026-05-18栏目: 教程
-
三元牛奶,的优势在哪里?
2026-05-18栏目: 教程
-
常浏览“成人网站”要留意了,有这2种反应,请立马关闭
2026-05-18栏目: 教程
-
美国SpaceX公司计划向太空发射4.2万枚通信卫星
2026-05-18栏目: 教程
