This code is a notepad but it cant save its entries. When you switch pages all of the text is erased. I tried to use userdefualts to save my text but I could not figure it out. My button is saveTheTable. Here is picture of my app.
import UIKit
class ViewControllerX: UIViewController, UITableViewDataSource{
@IBOutlet weak var inter: UIWebView!
var items: [String] = [""]
@IBOutlet weak var listTableViewXX: UITableView!
@IBAction func addX(_ sender: Any) {
alert()
}
override func viewDidLoad() {
super.viewDidLoad()
listTableViewXX.dataSource = self
let userDefaults = Foundation.UserDefaults.standard
userDefaults.set( "String", forKey: "Key")
var path = ""
path = Bundle.main.path(forResource: "command", ofType: "pdf")!
let url = URL(fileURLWithPath: path)
self.inter.loadRequest(URLRequest(url: url))
}
@IBAction func mamssmas(_ sender: Any) {
if let url = URL(string: "http://www.masstimes.org") {
UIApplication.shared.openURL(url)
}}
@IBAction func savethetable(_ sender: Any) {
//https://www.youtube.com/watch?v=h8QJda2-H44&list=LL-n-1GmbB2qqPzPkzO_nrRA&index=1&t=401s
//To retrieve from the key
let userDefaults = Foundation.UserDefaults.standard
let value = userDefaults.string(forKey: "Key")
print(value)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "listItemX" ) as! ItemTableViewCellX
cell.itemLabelX.text = items[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func alert(){
let alert = UIAlertController(title: "", message: "", preferredStyle: .alert)
alert.addTextField{
(textfield) in
textfield.placeholder = "Enter you sin"
}
let add = UIAlertAction(title: "Add", style: .default){
(action) in
let textfield = alert.textFields![0]
self.items.append(textfield.text!)
self.listTableViewXX.reloadData()
}
let cancel = UIAlertAction(title: "Cancel", style: .cancel) {
(alert) in
print("HI")
}
alert.addAction(add)
alert.addAction(cancel)
present(alert, animated: true, completion: nil)
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
items.remove(at: (indexPath as NSIndexPath).row)
tableView.deleteRows(at: [indexPath], with: .automatic)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}