簡體   English   中英

如何識別ios中的鍵盤按鍵?

[英]How can I recognize keyboard presses in ios?

我到處都在尋找答案,但是本質上,我試圖做的是當一個人按下其iPhone鍵盤上的冒號時,我想得到通知並執行某些操作。 我希望這是有道理的。 如果您提供答案,請記住我是一個相對較新的IOS開發人員:)

謝謝!

編輯:如果我的上述聲明不太合理,這將是理想情況:

  1. 用戶點擊文本框
  2. 用戶按數字1鍵
  3. 通知用戶已按下數字1鍵
  4. 而不是打印數字1,文本將替換為數字2。

這是一個簡單的例子。

這是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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM