我正在制作类似于iPhone上的快速笔记应用程序。但是,当你去写你的条目时,我有多个textViews
。当我进入textView
控制器和tableView
区域时,textViews
正常工作。我希望能够将数据保存在其他textViews
中,以便当用户输入注释或打开现有注释时。 tableview
( ViewController )和textViews
( DetailViewContoller )的代码如下所示。谢谢!
的ViewController
import UIKit
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
@IBOutlet weak var table: UITableView!
var data:[String] = []
var selectedRow:Int = -1
var newRowText:String = ""
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.title = "Notes"
let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addNote))
self.navigationItem.rightBarButtonItem = addButton
self.navigationItem.leftBarButtonItem = editButtonItem
load()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if selectedRow == -1 {
return
}
data[selectedRow] = newRowText
if newRowText == "" {
data.remove(at: selectedRow)
}
table.reloadData()
save()
}
func addNote() {
if (table.isEditing) {
return
}
let name:String = ""
data.insert(name, at: 0)
let indexPath:IndexPath = IndexPath(row: 0, section: 0)
table.insertRows(at: [indexPath], with: .automatic)
table.selectRow(at: indexPath, animated: true, scrollPosition: .none)
self.performSegue(withIdentifier: "detail", sender: nil)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell")!
cell.textLabel?.text = data[indexPath.row]
return cell
}
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
table.setEditing(editing, animated: animated)
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
data.remove(at: indexPath.row)
table.deleteRows(at: [indexPath], with: .fade)
save()
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "detail", sender: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let detailView:DetailViewController = segue.destination as! DetailViewController
selectedRow = table.indexPathForSelectedRow!.row
detailView.masterView = self
detailView.setText(t: data[selectedRow])
}
func save() {
UserDefaults.standard.set(data, forKey: "notes")
UserDefaults.standard.synchronize()
}
func load() {
if let loadedData = UserDefaults.standard.value(forKey: "notes") as? [String] {
data = loadedData
table.reloadData()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
DetailViewController
import UIKit
class DetailViewController: UIViewController {
@IBOutlet weak var textView: UITextView!
var text:String = ""
var text1:String = ""
@IBOutlet weak var textView1: UITextView!
var masterView:ViewController!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
textView.text = text
textView1.text = text1
}
func setText(t:String) {
text = t
if isViewLoaded {
textView.text = t
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
masterView.newRowText = textView.text
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}