【问题标题】:how to handle key events in iphone如何处理iphone中的关键事件
【发布时间】:2011-03-07 05:40:19
【问题描述】:

嗨 我正在开发一个 iphone 应用程序,并希望在 iphone 中处理键盘事件。在 Mac 中,有一个类 NSEvent 处理键盘和鼠标事件,而在 ios (iphone/ipad) 中,NSEvent 的对应物是 UIEvent,它只处理触摸事件。我知道 ios API 不提供此功能,但我如何处理 iphone 中的关键事件???任何好的教程或某事,开始......

【问题讨论】:

    标签: iphone objective-c ios macos nsevent


    【解决方案1】:

    您不能直接为键盘的键编码,并且设备没有鼠标。

    您可以为不同类型的字符集制作逻辑,也可以在 textField 委托方法或 Textview 委托方法中制作逻辑

    textView 委托

    - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
    

    textField 委托

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    

    您还可以对 textField 和 Textview 使用 Notification。

    TextField 使用这个

    在 viewDidLoad 中调用这个注册方法

    -(void)registerForTextFieldNotifications {
    
        NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
    
        [notificationCenter addObserver:self
                               selector:@selector (handle_TextFieldTextChanged:)
                                   name:UITextFieldTextDidChangeNotification
                                 object:self.textField];
    
    }
    
    
    - (void) handle_TextFieldTextChanged:(id)notification {
    
    
    
        if([iSinAppObj.passCodeString isEqualToString:lockTextField.text])
        {   
            //code here
        }
    
    }
    

    对于文本视图,您只需像这样更改事件名称

    [notificationCenter addObserver:self
                                   selector:@selector (handle_TextFieldTextChanged:)
                                       name:UITextViewTextDidChangeNotification
                                     object:self.textField];
    

    【讨论】:

      【解决方案2】:

      【讨论】:

      • 非常感谢...但是此链接似乎没有为我提供完整的功能...我想处理用户(在 iPhone 上)键入的每个键,并将其发送到另一台机器通过本地网络
      • 可能会使用隐藏的文本字段并通过委托捕获输入?
      • 是的,我想到了同样的解决方案......所以让我试试吧非常感谢你......干杯
      猜你喜欢
      • 1970-01-01
      • 2018-03-17
      • 1970-01-01
      • 2014-05-11
      • 2014-09-05
      • 2020-06-01
      • 2014-11-11
      • 2010-10-13
      • 2011-03-08
      相关资源
      最近更新 更多