0

Im making to do list-app which consist of 2 View Controllers one with table view hold an array and display it and second one with a textField, a button function to append the text field's text to the array to display the new appended string it in the first view controller heres my code:

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

@IBOutlet weak var tableView: UITableView!

var exmpArray = ["DDDD","rrr","TTT"]

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}
@IBAction func addBtnBar(_ sender: Any) {
    performSegue(withIdentifier: "showMe", sender: nil)
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
    cell?.textLabel?.text = exmpArray[indexPath.row]  
    return cell!
}}

and the second one is:

class SecondViewController: UIViewController {

@IBOutlet weak var myTextField: UITextField!

var realAry:[String] = []

override func viewDidLoad() {
    super.viewDidLoad()

}

let myObj = ViewController()

@IBAction func addBtn(_ sender: Any) {

    myObj.exmpArray.append(myTextField.text!)
    print(myObj.exmpArray)        

}

after appending the new words it doesn't display in the first controller

2 Answers 2

2

One problem is this line:

let myObj = ViewController()

That creates a new instance of ViewController that has nothing to do with the view controller that called you. Get rid of that line. It's wrong.

You need to set up a delegate property in SecondViewController that points back to ViewController, define a protocol for that delegate to conform to, and use that protocol so that SecondViewController can notify it's delegate when the array changes.

In ViewController, add a prepareForSegue() function that takes the destination view controller, casts it to type SecondViewController, and sets SecondViewController's delegate to self.

Sign up to request clarification or add additional context in comments.

Comments

1

Both arrays are different you are just copying first VC array to 2nd VC array. Array is value type in swift. Instead of creating 2 arrays make 1st VC array as static.

static var exmpArray = ["DDDD","rrr","TTT"]

Now you can use & update this in all VC by calling like below

FirstVC.exmpArray

2 Comments

thanks that worked --- i can see the new values when i print it in the console but now i only need to use tableView.reloadData() to show it in the table ... can you please advice where i can implement this function in my class ?
Who downvoted this? Its a reasonable answer, and provides useful information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.