ボタンを押したときにcountLabel2の値を次の画面のtableviewの保存したいのですが今のままのコードでは保存されません。解説策を教えていただきたいです。
ResultViewControllar.swift
swift
1@IBAction func record(_ sender: Any) { 2 3 //配列を取り出す 4 if (UserDefaults.standard.object(forKey: "save") != nil) { 5 array = UserDefaults.standard.object(forKey: "save") as! [String] 6 } 7 8 array.append(countLabel2.text!) 9 UserDefaults.standard.set(array, forKey: "save") 10 }
SaveviewController
Swift
1import UIKit 2 3class SaveViewController: UIViewController,UITableViewDelegate, 4UITableViewDataSource { 5 6 @IBOutlet var tableView: UITableView! 7 8 var resultArray = [String]() 9 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 14 tableView.delegate = self 15 tableView.dataSource = self 16 17 if UserDefaults.standard.object(forKey: "save") != nil { 18 resultArray = UserDefaults.standard.object(forKey: "save") as! [String] 19 } 20 tableView.reloadData() 21 22 23 } 24 25 26 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 27 return resultArray.count 28 } 29 30 func numberOfSections(in tableView: UITableView) -> Int { 31 return 1 32 } 33 34 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 35 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 36 37 cell.textLabel?.text = resultArray[indexPath.row] 38 39 return cell 40 } 41 42 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 43 return 60 44 } 45 46 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { 47 48 if editingStyle == .delete { 49 resultArray.remove(at: indexPath.row) 50 51 UserDefaults.standard.set(resultArray, forKey: "todo") 52 53 tableView.reloadData() 54 } 55 } 56
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。