2017-10-08 72 views
0

我以前的工作應用程序,您可以添加食品項目清單的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) 
} 
} 

這裏是界面生成器的PIC - enter image description here

它運行約30分鐘之前,我就遇到了這個錯誤。我能夠添加一些食物,但現在我不能

enter image description here

回答

0

同樣的事情也發生在我身上,因爲Xcode的9

textfield.inputAssistantItem.leadingBarButtonGroups = [] 
textfield.inputAssistantItem.trailingBarButtonGroups = [] 

設置BarButtonGroups這樣做的伎倆。

0

我想通了是什麼!

這是阻止我的交互的容器視圖。

不知道什麼工作只是暫時的,但會弄明白!