Search code examples
iosswiftuitableviewdelete-rowediting

Editing a UITableView with a button from another view


I have two views, one with a button where I want when the button is pressed to get the other view with a UITableView to be in a Editing mode. Here is my code, the editing works when I put the second view code in viewDidLoadbut dosent work when I call it from the first view.

First View Code:

@IBAction func addButtClick(_ sender: UIButton) {

    let vc = GrowthMainViewController()
    vc.navigationItem.rightBarButtonItem = self.editButtonItem
    vc.editbuttpressed()     

}

Second View (with the tableView) code:

 func editbuttpressed() {

    self.scheduleTableView.isEditing = true
    self.goalsTableView.isEditing = true

}

Solution

  • This issue arises from trying to access a tableView that is an IBOutlet. The destination view controller's outlets is not created yet which that's why all IBOutlets would be nil.

    The resolution for this was to create a boolean variable in the destination view...

    var tempBoolean: Bool? = false
    

    You can then assign that variable's value to true in the first viewController, which will work because it is initialized with a value.

    Finally, in your destination view enable editing depending on that boolean variable's value in viewDidLoad...

    if (tempBoolean == true) { 
       scheduleTableView.isEditing = true
       goalsTableView.isEditing = true
    }
    

    Something along those lines resolve this issue.