// 继承与uiviewcontroller的类中写需要添加的数据并回传回去
class JumpViewController: UIViewController {
var button:UIButton?
var textfild:UITextField?
// 协议
var paramsProtocolDelegate: ParamsProtocol?
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.white
textfild = UITextField(frame: CGRect(x: 40, y: 150, width: self.view.frame.size.width - 80, height: 50))
textfild?.placeholder = "输入添加的内容"
button = UIButton(type: .custom)
button?.frame = CGRect(x: 40, y: 250, width: self.view.frame.size.width - 80, height: 50)
button? .setTitle("确定添加", for: .normal)
button? .setTitleColor(UIColor.white, for: .normal)
button?.backgroundColor = UIColor.blue
button?.addTarget(self, action: #selector(confrom), for: .touchUpInside)
self .view.addSubview(button!)
self.view .addSubview(textfild!)
}
// 点击按钮方法
@objc func confrom(){
self .start()
self.navigationController?.popViewController(animated: true)
}
// 定义协议用于回传之后另一个页面调用
protocol ParamsProtocol {
func returnparam(tmpstr:String)
}
// 数据回传
func start(){
let str = textfild?.text
// 给回传方法赋值
self.paramsProtocolDelegate?.returnparam(tmpstr: str!)
}
}
// 第二部 viewcontroller中添加表格和导航栏按钮 点击添加按钮可以跳转到另一个页面,然后进行数据传值//
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,ParamsProtocol{
// 遵循协议实现添加
func returnparam(tmpstr: String) {
addStr = tmpstr
print(addStr)
self.TabArr?.addObjects(from: [addStr])
self.tableview?.reloadData()
}
var addStr = String()
var tableview:UITableView?
var TabArr:NSMutableArray?
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (TabArr?.count)!
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: nil)
cell.textLabel?.text = TabArr?[indexPath.row] as? String
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
// 添加导航栏按钮
let AddbarButton = UIBarButtonItem(title: "添加", style: UIBarButtonItemStyle.plain, target: self, action: #selector(add))
self.navigationItem.rightBarButtonItem = AddbarButton
// let DeletebarButton = UIBarButtonItem(title: "删除", style: UIBarButtonItemStyle.plain, target: self, action: #selector(delet))
// self.navigationItem.leftBarButtonItem = DeletebarButton
TabArr = NSMutableArray.init(array: ["1","2","3","4","5","6","7","8",])
tableview = UITableView.init(frame: self.view.frame, style: .plain)
tableview?.delegate = self
tableview?.dataSource = self
self.view.addSubview(tableview!)
}
// 方法删除
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
TabArr?.remove(indexPath.row)
if editingStyle == UITableViewCellEditingStyle.delete {
self.TabArr?.remove(self.TabArr![indexPath.row])
}
self.tableview?.reloadData()
}
// 点击添加按钮
@objc func add(){
let jump = JumpViewController()
jump.paramsProtocolDelegate = self
// 遵循代理
self.navigationController?.pushViewController(jump, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}