[英]How can I recognize keyboard presses in ios?
我到處都在尋找答案,但是本質上,我試圖做的是當一個人按下其iPhone鍵盤上的冒號時,我想得到通知並執行某些操作。 我希望這是有道理的。 如果您提供答案,請記住我是一個相對較新的IOS開發人員:)
謝謝!
編輯:如果我的上述聲明不太合理,這將是理想情況:
這是一個簡單的例子。
這是UITextField的委托方法的示例,如果用戶嘗試輸入大寫字符,它將代替小寫字符出現:
-(BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string {
NSString* lc = [string lowercaseString];
if ([string isEqualToString:lc])
return YES;
textField.text =
[textField.text stringByReplacingCharactersInRange:range
withString:lc];
return NO;
}
您應該能夠針對您的特定用例執行類似的操作。
如前所述,使用此回調並在其中進行更改:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//check here if the new character is the one you are looking for
if ([string isEqualToString:@"a"])
{
//create a new string with the character you want to use instead
NSString *newText = [textField.text stringByReplacingCharactersInRange:range withString:@"A"];
//set it as the text for your text field
[textField setText:newText];
return NO;
}
return YES;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.