Skip to main content
レイアウト編集と、タグの追加
ソース リンク
nagonsoftware
  • 1,921
  • 2
  • 10
  • 13

import UIKit
import Foundation

class PostVC: UIViewController {

import UIKit
import Foundation

class PostVC: UIViewController {
    
    @IBOutlet weak var textField: UITextField!
    
    var data: [String] = []
    
    let userDefaults = UserDefaults.standard
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    @IBAction func cellAdd(_ sender: Any) {
        guard let list = tabBarController?.viewControllers?[0] as? ListVC else {
            print("Cannot get listVC from tab bar controller")
            return
        }
        
        let postText = textField.text ?? ""
        list.data.insert(postText, at: 0)
        
        userDefaults.set(data, forKey: "DataStore")
        
        userDefaults.synchronize()

        list.tableView?.insertRows(at: [IndexPath(row: 0, section: 0)], with: .automatic)
    }
    
}

}

List

import UIKit

class ListVC: UIViewController, UITableViewDataSource, UITableViewDelegate {

import UIKit

class ListVC: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
    @IBOutlet weak var tableView: UITableView!
    
    var data: [String] = []
    
    let userDefaults = UserDefaults.standard
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        // <TableView初期設定>
        tableView.dataSource = self
        tableView.delegate = self
        
        data = readData()
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let postcell = tableView.dequeueReusableCell(withIdentifier: "postcell")!
        postcell.textLabel?.text = data[indexPath.row]
        return postcell
    }
    
    func readData() -> [String] {
        let dataArray = userDefaults.array(forKey: "DataStore") as? [String] ?? []
        return dataArray
    }
    
}

}

import UIKit
import Foundation

class PostVC: UIViewController {

@IBOutlet weak var textField: UITextField!

var data: [String] = []

let userDefaults = UserDefaults.standard

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

@IBAction func cellAdd(_ sender: Any) {
    guard let list = tabBarController?.viewControllers?[0] as? ListVC else {
        print("Cannot get listVC from tab bar controller")
        return
    }
    
    let postText = textField.text ?? ""
    list.data.insert(postText, at: 0)
    
    userDefaults.set(data, forKey: "DataStore")
    
    userDefaults.synchronize()

    list.tableView?.insertRows(at: [IndexPath(row: 0, section: 0)], with: .automatic)
}

}

List

import UIKit

class ListVC: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var tableView: UITableView!

var data: [String] = []

let userDefaults = UserDefaults.standard

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    
    // <TableView初期設定>
    tableView.dataSource = self
    tableView.delegate = self
    
    data = readData()
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return data.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let postcell = tableView.dequeueReusableCell(withIdentifier: "postcell")!
    postcell.textLabel?.text = data[indexPath.row]
    return postcell
}

func readData() -> [String] {
    let dataArray = userDefaults.array(forKey: "DataStore") as? [String] ?? []
    return dataArray
}

}

import UIKit
import Foundation

class PostVC: UIViewController {
    
    @IBOutlet weak var textField: UITextField!
    
    var data: [String] = []
    
    let userDefaults = UserDefaults.standard
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    @IBAction func cellAdd(_ sender: Any) {
        guard let list = tabBarController?.viewControllers?[0] as? ListVC else {
            print("Cannot get listVC from tab bar controller")
            return
        }
        
        let postText = textField.text ?? ""
        list.data.insert(postText, at: 0)
        
        userDefaults.set(data, forKey: "DataStore")
        
        userDefaults.synchronize()

        list.tableView?.insertRows(at: [IndexPath(row: 0, section: 0)], with: .automatic)
    }
    
}

List

import UIKit

class ListVC: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
    @IBOutlet weak var tableView: UITableView!
    
    var data: [String] = []
    
    let userDefaults = UserDefaults.standard
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        // <TableView初期設定>
        tableView.dataSource = self
        tableView.delegate = self
        
        data = readData()
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let postcell = tableView.dequeueReusableCell(withIdentifier: "postcell")!
        postcell.textLabel?.text = data[indexPath.row]
        return postcell
    }
    
    func readData() -> [String] {
        let dataArray = userDefaults.array(forKey: "DataStore") as? [String] ?? []
        return dataArray
    }
    
}
ソース リンク
tony
  • 117
  • 17

Swift 他画面からのUserDefaults

SwiftのUserDefaultsにおいて

Tab ControllerでつないだListとPostにおいて

PostでUserDefaultsしたdataが再度立ち上げたときにListで表示されません

どうしたらいいでしょうか?

Post

import UIKit
import Foundation

class PostVC: UIViewController {

@IBOutlet weak var textField: UITextField!

var data: [String] = []

let userDefaults = UserDefaults.standard

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

@IBAction func cellAdd(_ sender: Any) {
    guard let list = tabBarController?.viewControllers?[0] as? ListVC else {
        print("Cannot get listVC from tab bar controller")
        return
    }
    
    let postText = textField.text ?? ""
    list.data.insert(postText, at: 0)
    
    userDefaults.set(data, forKey: "DataStore")
    
    userDefaults.synchronize()

    list.tableView?.insertRows(at: [IndexPath(row: 0, section: 0)], with: .automatic)
}

}

List

import UIKit

class ListVC: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var tableView: UITableView!

var data: [String] = []

let userDefaults = UserDefaults.standard

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    
    // <TableView初期設定>
    tableView.dataSource = self
    tableView.delegate = self
    
    data = readData()
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return data.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let postcell = tableView.dequeueReusableCell(withIdentifier: "postcell")!
    postcell.textLabel?.text = data[indexPath.row]
    return postcell
}

func readData() -> [String] {
    let dataArray = userDefaults.array(forKey: "DataStore") as? [String] ?? []
    return dataArray
}

}