G26Bandit
G26Bandit

Reputation: 81

Keyboard Won't Stay Up on Textfield When Called From Swipe Action (Swift/Xcode)

I have a tableview, and the cells have a textfield in them. I'd like to make this textfield uneditable unless the user taps a swipe action. In that case, I'd like the keyboard to come up automatically on the textfield in the cell they want to edit.

Following a YouTube tutorial (for setting up swipe actions), I thought I had it all set. However, when I test it, the keyboard comes up for a second and then goes back down all on its own. It won't stay up. The only thing I can figure is that something is taking the first responder away from the textfield, but I don't know how to check that.

Everything seems to work just fine, other than the keyboard refusing to stay up. I searched online and couldn't find anything on it. I will admit I'm still learning completion handlers, and it's still a little confusing. If someone could point me in the right direction, it would be much appreciated.

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    
    let renameItem = UIContextualAction(style: .normal, title: "Rename") { contextAction, viewProperty, completionHandler in
        
        let cell = tableView.cellForRow(at: indexPath) as! ItemTableViewCell
        cell.nameField.isUserInteractionEnabled = true
        cell.nameField.becomeFirstResponder()
        completionHandler(true)
        
    }
    
       return UISwipeActionsConfiguration(actions: [renameItem])
    
}

Upvotes: 1

Views: 58

Answers (0)

Related Questions