In my swift code below the goal is to save a string to a core data relationship. In class two there is a func that adds a string to the tableview in that class. But that added string is only displayed under the name that is selected in parent class View controller. I have added a gif below to show what I am looking for.
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var managedObjectContext: NSManagedObjectContext?
var items : [Person]?
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let person = self.items?[indexPath.row].name {
let vcx = Two()
vcx.valueMove = person // Assign the name to valueMove property
vcx.modalPresentationStyle = .overCurrentContext // or .fullScreen
self.present(vcx, animated: true)
}
}
}
class Two: UIViewController, UITableViewDataSource, UITableViewDelegate {
var nameLabel = UILabel()
var twoTableA = UITableView()
var addB = UIButton()
var data = [String]() // Data source for the table view
var backButton = UIButton()
var valueMove = String()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
[twoTableA, addB,nameLabel,backButton].forEach {
view.addSubview($0)
$0.translatesAutoresizingMaskIntoConstraints = false
$0.layer.borderWidth = 1
}
addB.backgroundColor = .purple
nameLabel.text = valueMove
NSLayoutConstraint.activate([
nameLabel.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
nameLabel.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.10, constant: 0),
nameLabel.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1, constant: 0),
nameLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor),
twoTableA.topAnchor.constraint(equalTo: nameLabel.bottomAnchor),
twoTableA.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.40, constant: 0),
twoTableA.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1, constant: 0),
twoTableA.leadingAnchor.constraint(equalTo: view.leadingAnchor),
addB.topAnchor.constraint(equalTo: twoTableA.bottomAnchor),
addB.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.10, constant: 0),
addB.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1, constant: 0),
addB.leadingAnchor.constraint(equalTo: view.leadingAnchor),
backButton.topAnchor.constraint(equalTo: addB.bottomAnchor),
backButton.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.10, constant: 0),
backButton.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1, constant: 0),
backButton.leadingAnchor.constraint(equalTo: view.leadingAnchor),
])
backButton.backgroundColor = .systemTeal
backButton.setTitle("back", for: .normal)
addB.setTitle("Add", for: .normal)
addB.addTarget(self, action: #selector(addJohnson), for: .touchUpInside)
backButton.addTarget(self, action: #selector(back), for: .touchUpInside)
twoTableA.dataSource = self
twoTableA.delegate = self
twoTableA.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
}
var managedObjectContext: NSManagedObjectContext?
@objc func back(){
let controller = ViewController() // Assuming HomeVC is the view controller class you want to present
controller.managedObjectContext = self.managedObjectContext
controller.modalPresentationStyle = .fullScreen // or .overFullScreen or any other style you prefer
self.dismiss( animated: true, completion: nil)
}
@objc func addJohnson() {
data.append("Family Member")
twoTableA.reloadData()
}
// MARK: - UITableViewDataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.text = data[indexPath.row]
return cell
}
}