1
votes

I have two view controllers one with the 3 tableviews and other controller I have a uitextfield and the text entered in the text field I want to add it to one of the tableview called ScheduleTableView in the other view controller. Here is my code but I get the error unexpectedly found nil while unwrapping an optional value at vc.ScheduleTableView.beginUpdates()

@IBAction func addButtonTapped(_ sender: YTRoundedButton) {
   self.performSegue(withIdentifier: "secondvc", sender: self)

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier  == "secondvc" {
        print(TitleTextField.text!)

        let vc = segue.destination as! GrowthMainViewController

        vc.ScheduleArray.append(TitleTextField.text!)
        let indexPath = IndexPath(row: vc.ScheduleArray.count - 1, section: 0)
        vc.ScheduleTableView.beginUpdates()
        vc.ScheduleTableView.insertRows(at: [indexPath], with: .automatic)
        vc.ScheduleTableView.reloadData()
        vc.ScheduleTableView.endUpdates()



        TitleTextField.text = ""
        view.endEditing(true)

  }     
}
3
I tried also only calling a function when the button is pressed and update the tableview in the other view but didn't workedCoderJ13
Check the updated code, but still I have the same errorCoderJ13
Unrelated to your issue but please note that variable and function names should start with lowercase letters. Class names start with uppercase letters.rmaddy
I want the viewcontroller to update when button in the first view is pressed so where should I put the code in the second view without using vc.AddSchedule() CoderJ13
Like you said to me I passed the data using let vc = segue.destination as! GrowthMainViewController vc.ScheduleTitle = TitleTextField.text! to the other view but in the other view how can I update the view knowing that the button is pressed in the first viewCoderJ13

3 Answers

1
votes

The resolution for this was changing where the array was declared (singleton is fine for this), and removing the unnecessary inserting, updating, reloading, etc.

The numRowsInSection method then calls the scheduleArray.count to show all corresponding data.

Before:

@IBAction func addButtonTapped(_ sender: YTRoundedButton) {
   self.performSegue(withIdentifier: "secondvc", sender: self)

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier  == "secondvc" {
        print(TitleTextField.text!)

        let vc = segue.destination as! GrowthMainViewController

        vc.ScheduleArray.append(TitleTextField.text!)
        let indexPath = IndexPath(row: vc.ScheduleArray.count - 1, section: 0)
        vc.ScheduleTableView.beginUpdates()
        vc.ScheduleTableView.insertRows(at: [indexPath], with: .automatic)
        vc.ScheduleTableView.reloadData()
        vc.ScheduleTableView.endUpdates()



        TitleTextField.text = ""
        view.endEditing(true)

  }     
}

After :

@IBAction func addButtonTapped(_ sender: YTRoundedButton) {
   self.performSegue(withIdentifier: "secondvc", sender: self)

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier  == "secondvc" {

        guard let text = TitleTextField.text else { return }
        scheduleArray.append(text)
        TitleTextField.text = ""
        view.endEditing(true)

  }     
}
0
votes

vc.ScheduleArray.count - 1 is probably a negative indexpath

try this

if (vc.ScheduleArray.count - 1 >= 0){
    vc.ScheduleTableView.insertRows(at: [indexPath], with: .automatic)
}
0
votes

The problem is that you trying to update a view controller inside of prepare function. Inside that function, the view is instantiated but it's Outlets are not connected yet.

To solve that issue, follow these steps:

On this method you should first update your model:

@IBAction func addButtonTapped(_ sender: YTRoundedButton) {
   // update your model here

   self.performSegue(withIdentifier: "secondvc", sender: self)    
}

In your destination view controller, you should handle this model change and reload the table view's data.

override func viewDidLoad() {
    self.tableView.reloadData()
}