textfieldのキーボードを強制的に閉じようとしているのですがうまくいきません。
現在tableviewcellの中にtextfieldがあり、textFieldDidBeginEditingメソッドの中でendEditingやresignFirstResponderを呼んでいるのですがキーボードが閉じません。
現在のtableviewcellクラスのコードは以下のようになっています。デリゲートはStoryboardから引っ張ってきています。
swift
1class SettingEditTableViewCountryCell: UITableViewCell, UITextFieldDelegate { 2 3 @IBOutlet weak var countryTextField: UITextField! 4 5 override func awakeFromNib() { 6 super.awakeFromNib() 7 } 8 9 override func setSelected(_ selected: Bool, animated: Bool) { 10 super.setSelected(selected, animated: animated) 11 } 12 13 // MARK: - UITextFieldDelegate 14 15 func textFieldDidBeginEditing(_ textField: UITextField) { 16 self.endEditing(true) 17 } 18 19}
画面には他にもtextfieldがあり、textfieldがタップされた時にワケあって一つ前に表示されていたのtextfieldのキーボードを閉じたいのですが、どうすればキーボードを閉じることができるでしょうか?
どなたかわかる方がいれば教えていただきたいです。よろしくお願いします。
///追記///
swift
1// MARK :- UITableViewDataSource 2 3 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 4 return self.displayCells().count 5 } 6 7 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 8 let id = displayCells()[indexPath.row].rawValue 9 let cell = tableView.dequeueReusableCell(withIdentifier: id, for: indexPath) 10 self.configureCell(targetCell: cell, indexPath: indexPath) 11 12 return cell 13 } 14 15 func configureCell(targetCell: UITableViewCell, indexPath: IndexPath) { 16 let type = displayCells()[indexPath.row] 17 18 switch type { 19 case .image: 20 guard let cell = targetCell as? SettingEditTableViewImageCell else { 21 return 22 } 23 24 if let image = SettingManager.sharedInstance.settings["image"]?.flatMap({$0}) { 25 if let imageString = image as? String { 26 cell.imageButton.setImage(Converter.sharedInstance.convertStringToImage(imageString: imageString), for: .normal) 27 } 28 } else { 29 cell.imageButton.setImage(UIImage(named: "camera"), for: .normal) 30 } 31 32 case .name: 33 if let name = SettingManager.sharedInstance.settings["name"]?.flatMap({$0}) { 34 if let cell = targetCell as? SettingEditTableViewTextCell { 35 cell.textField.text = name as? String 36 } 37 } 38 39 case .country: 40 if let country = SettingManager.sharedInstance.settings["country"]?.flatMap({$0}) { 41 if let cell = targetCell as? SettingEditTableViewCountryCell { 42 cell.countryTextField.text = country as? String 43 } 44 } 45 46 case .comment: 47 if let comment = SettingManager.sharedInstance.settings["comment"]?.flatMap({$0}) { 48 if let cell = targetCell as? SettingEditTableViewTextCell { 49 cell.textField.text = comment as? String 50 } 51 } 52 } 53 } 54
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/12/27 23:00
2016/12/27 23:08
2016/12/28 12:11