版本:
swift 4,ios 11.2
1.选Tabbed APP
2.制作视图界面
3.加入TableView
4.配对FirstView
5.调整颜色
6.配对输入框和按钮
7.调整键盘和按钮
8.再配对一次文字输入框
9.输入待办事项
10.将资料存入手机-1
11.将资料存入手机-2
12.添加删除功能
13.添加编辑功能
14.第一个画面的代码
// FirstViewController.swift
// ToDoList
//
// Created by lin on 2018/3/28.
// Copyright © 2018年 lin. All rights reserved.
//
import UIKit
class FirstViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
override var preferredStatusBarStyle: UIStatusBarStyle{
return .lightContent
}
@IBOutlet weak var myTableView: UITableView!
var toDos = UserDefaults.standard.stringArray(forKey: "todos") ?? [String]()
override func viewDidLoad() {
super.viewDidLoad()
myTableView.dataSource = self
myTableView.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return toDos.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = toDos[indexPath.row]
cell.textLabel?.textColor = .white
cell.textLabel?.font = UIFont(name: "arial", size: 24)
return cell
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete{
toDos.remove(at: indexPath.row)
UserDefaults.standard.set(toDos, forKey: "todos")
myTableView.reloadData()
}
}
func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool { //取消选中高亮
return false
}
func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) { //当按下新增图标后会转到第二个画面
if let secondViewController = tabBarController?.viewControllers?[1] as? SecondViewController{
secondViewController.infoFromViewOne = indexPath.row
}
tabBarController?.selectedIndex = 1
}
}
15.第二个画面的代码
// SecondViewController.swift
// ToDoList
//
// Created by lin on 2018/3/28.
// Copyright © 2018年 lin. All rights reserved.
//
import UIKit
class SecondViewController: UIViewController, UITextFieldDelegate {
var infoFromViewOne:Int? //接收第一个画面的值
override var preferredStatusBarStyle: UIStatusBarStyle{
return .lightContent
}
@IBOutlet weak var myTaxtInput: UITextField!
@IBAction func textFieldDidChanged(_ sender: UITextField) {
if sender.text != ""{
myButton.setTitle("OK", for: .normal)
}else{
myButton.setTitle("Back", for: .normal)
}
}
@IBAction func buttonPressed(_ sender: UIButton) { //增加编辑功能
if let text = myTaxtInput.text{
guard let firstViewController = tabBarController?.viewControllers?[0] as? FirstViewController
else{return}
if text != ""{
if infoFromViewOne != nil{
firstViewController.toDos[infoFromViewOne!] = text
infoFromViewOne = nil
}else{
firstViewController.toDos.append(text)
}
firstViewController.myTableView.reloadData()
UserDefaults.standard.set(firstViewController.toDos, forKey: "todos")
}else{
if infoFromViewOne != nil{
firstViewController.toDos.remove(at: infoFromViewOne!)
firstViewController.myTableView.reloadData()
UserDefaults.standard.set(firstViewController.toDos, forKey: "todos")
infoFromViewOne = nil
}
}
}
myTaxtInput.text = ""
self.tabBarController?.selectedIndex = 0
}
@IBOutlet weak var myButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
myTaxtInput.becomeFirstResponder()
myTaxtInput.delegate = self
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if infoFromViewOne != nil{
if let firstViewController = tabBarController?.viewControllers?[0] as? FirstViewController{
myTaxtInput.text = firstViewController.toDos[infoFromViewOne!]
myButton.setTitle("OK", for: .normal)
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool { //按下Return后的作用等同于按下OK按钮
buttonPressed(UIButton())
return true
}
}