我在这里和iOS世界都是个菜鸟。在我非常简单的待办事项列表iOS应用程序中,我很难在一个特定的情况下拒绝键盘。
当用户点击当前文本字段或键盘本身以外的任何位置时,我希望键盘被取消。到目前为止,当用户点击UITableView或我的应用程序中的大多数元素时,我的键盘就会很好地被丢弃(多亏了这里的堆叠溢出)。然而,当用户点击另一个UITextField时,键盘不会离开。
FYI,这是我到目前为止研究过但还没有解决这个问题的现有线程列表。( 1) How to dismiss keyboard iOS programmatically 2) Resigning First Responder for multiple UITextFields 3) Dismissing the First Responder/Keyboard with multiple Textfields 4) (至少再多几个,但我迷失了方向:( )
我到目前为止所做的事情如下:
(in viewDidLoad())
// Add 'tap' gesture to dismiss keyboard when done adding/editing to-do item
var tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tapOutside:")
tap.cancelsTouchesInView = true
self.view.addGestureRecognizer(tap)
func tapOutside(tapOutside: UIGestureRecognizer) {
// Dismiss keyboard
self.view.endEditing(true)
}
@IBAction func EditingDidBegin(sender: UITextField) {
// Highlight the text field which user is editing
self.highlightTextField(sender, highlight: true)
}
@IBAction func EditingDidEnd(sender: UITextField) {
// Undo text field highlight
self.highlightTextField(sender, highlight: false)
self.view.endEditing(true) // try this option and not working
self.setEditing(false, animated: true) // try this option and not working
sender.resignFirstResponder() // try this option and not working
UIApplication.sharedApplication().becomeFirstResponder() // try this option and not working
... // below is my code to update the todo item
}我还试图打印出我的视图的所有subviews.isFirstResponder()。所有这些都是假的。我也尝试过覆盖我的UIViewController的UIViewController,在里面它只调用self.view.endEditing(真)并调用其超级的,这也不起作用。
请帮帮忙。:(
蒂娅!
更新:你们真是太棒了!:多亏了你们,我才能让它发挥作用。当我学习新的框架时,有几个错误/混乱。所以我就是这么做的。
1)没有正确设置UITextField委托。错误:我在xcode中引用了文本字段,并将我的viewController链接为委托,并认为这应该能解决问题。我仍然需要研究和了解更好的原因。解决方案:我删除了ctrl-拖动链接,并在tableView:cellForRowAtIndexPath中显式调用myTextField.delegate = self。这就成功了。谢谢@Sidewalker
2)错误:我混合了textFieldShouldBeginEditing等和@IBAction。因此,我陷入了textFieldShouldBeginEditing接到电话的情况,但EditingDidBegin没有接到电话。解决方案:一旦我显式地设置委托= self并坚持实现textField.方法,而不对textField使用任何@IBAction,一切都是正常的。
发布于 2015-04-03 20:26:25
这里有一个选择..。当对另一个textField的编辑尝试开始时,我们将添加一个布尔标志来确定我们是否在一个textField中
使您的类遵守UITextFieldDelegate
class MyClass: UIViewController, UITextFieldDelegate别忘了设置委托,我们也会添加标志
myTextField.delegate = self
var inField = false实现"textFieldShouldBeginEditing“和"textFieldDidBeginEditing”
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
if inField {
inField = false
return false
}
return true
}
func textFieldDidBeginEditing(textField: UITextField) {
inField = true
}我更喜欢跟踪这样的事情,而不是识别子视图,因为它允许在其他地方使用标记并降低代码复杂性。
发布于 2015-04-03 20:12:28
好吧,键盘不会消失,因为它不希望这样做。新的UITextField只是成为第一个响应者,而其他人辞职。如果你不希望一个textField成为第一个响应者,如果另一个已经是,你将不得不切断它之前,它得到机会。我会尝试实现textFieldShouldBeginEditing并找出那里的逻辑。
我不喜欢这样子,但这应该会起到一些作用。
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
for subView in self.view.subviews{
if(subView.isKindOfClass(UITextField)){
if(subView.isFirstResponder()){
subView.resignFirstResponder();
return false;
}
}
}
return true;
}发布于 2015-04-03 20:31:22
首先,将所有UITextField (您正在创建的)委托设置为self,并创建一个UITextField成员变量。现在实现"textFieldDidBeginEditing“委托方法,并将textfield分配给成员UITextField变量。如下所示
func textFieldDidBeginEditing(textField: UITextField) {
yourMemberVariable = textField;
}所以现在,每当您想要取消键盘时,请调用"yourMemberVariable“对象上的call方法。应该管用的!!
https://stackoverflow.com/questions/29438850
复制相似问题