Xcode 6.3.在实现UITextFieldDelegate协议的类中,我想重写touchesBegan()方法以隐藏键盘.如果我在函数规范中避免编译器错误,那么尝试从Set或NSSet读取"touch"时会出现编译错误,否则super.touchesBegan(touches,withEvent:event)会抛出错误.在Xcode 6.2中编译的这些组合之一!(那么Swift"Set"的文档在哪里以及如何从一个元素中获取元素?)
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
// Hiding the Keyboard when the User Taps the Background
if let touch = touches.anyObject() as? UITouch {
if nameTF.isFirstResponder() && touch.view != nameTF {
nameTF.resignFirstResponder();
}
}
super.touchesBegan(touches , withEvent:event)
}
Run Code Online (Sandbox Code Playgroud)
尝试:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) or
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent)
Run Code Online (Sandbox Code Playgroud)
编译器错误:使用选择器'touchesBegan:withEvent:'的覆盖方法具有不兼容的类型'(NSSet,UIEvent) - >()'和
super.touchesBegan(touches , withEvent:event)
Run Code Online (Sandbox Code Playgroud)
还抱怨
'NSSet'不能隐式转换为'Set'; 你的意思是使用'as'来明确转换?
尝试:
override func touchesBegan(touches: Set<AnyObject>, withEvent event: UIEvent)
Run Code Online (Sandbox Code Playgroud)
编译器错误:类型'AnyObject'不符合协议'Hashable'
尝试:
override func …Run Code Online (Sandbox Code Playgroud) 我有一个文本字段,我想插入一个电话号码.问题是我使用数字键盘,没有任何输入键,我没有该键,我不知道如何关闭文本字段.
是否有任何方法可以检查文本字段中有多少个字符,并在有字符时关闭数字键盘?
对不起我的英语和抱歉的noob问题,但我刚开始与ObjC.
谢谢*
对不起伙计们,我忘了告诉你我已经尝试过了:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField.text.length== 9) {
[textField resignFirstResponder];
}
return YES;
}
Run Code Online (Sandbox Code Playgroud) 我有两个文本字段,一个用于用户名,第二个用于移动电话号码
用户名:我可以使用此方法隐藏用户名Text-Fields的键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是手机号码:
如何隐藏手机号码文字字段?