我以前的工作應用程序,您可以添加食品項目清單的UITextField停止對用戶交互
出於某種原因給出UITextField停止服用輸入 - 一切作品除了此UITextField。
我試過了.showasfirstresponder()方法,它仍然沒有解決問題 - 現在添加食物按鈕停止工作。
這似乎是一些棘手的問題,但由於我的應用程序在某個時間點工作起來很小。
這裏是我的相關代碼 -
視圖控制器 -
class ViewController: UIViewController, UITableViewDataSource,
UITableViewDelegate,
UITextFieldDelegate{
// instance vars
@IBOutlet weak var addFoodsTextField: UITextField!
@IBOutlet weak var tableView: UITableView!
var foods:[String] = []
var cellStyleForEditing: UITableViewCellEditingStyle = .none
let defaults = UserDefaults.standard
override func viewDidLoad() {
super.viewDidLoad()
// nsuserdefaults setUp
foods = defaults.stringArray(forKey: "foods") ?? [String]()
setUpTableView()
//addFoodsTextField.becomeFirstResponder()
}
func setUpTableView() {
let containerView:UIView = UIView(frame:tableView.bounds)
containerView.backgroundColor = UIColor.clear
containerView.layer.shadowOffset = CGSize(width: 5.0, height: 5.0)
containerView.layer.shadowOpacity = 0.5
containerView.layer.shadowRadius = 5
let nibName = UINib(nibName: "TableViewCell", bundle: nil)
tableView.register(nibName, forCellReuseIdentifier: "tableViewCell")
tableView.dataSource = self
tableView.delegate = self
tableView.tableFooterView = UIView(frame: CGRect.zero)
tableView.separatorStyle = .none
tableView.showsVerticalScrollIndicator = false
tableView.layer.cornerRadius = 10
tableView.layer.masksToBounds = true
view.addSubview(containerView)
containerView.addSubview(tableView)
}
@IBAction func addButtonTapped(_ sender: Any) {
insertNewFoodTitle()
}
func insertNewFoodTitle() {
foods.append(addFoodsTextField.text!)
let indexPath = IndexPath(row: foods.count - 1, section: 0)
tableView.beginUpdates()
tableView.insertRows(at: [indexPath], with: .automatic)
defaults.set(foods, forKey: "foods")
tableView.endUpdates()
// set textFeild back to empty string
addFoodsTextField.text = ""
view.endEditing(true)
}
}
它運行約30分鐘之前,我就遇到了這個錯誤。我能夠添加一些食物,但現在我不能