简体   繁体   English

使用UIKeyCommand检测删除密钥

[英]Detect delete key using UIKeyCommand

任何人都知道如何在iOS 7上使用UIKeyCommand检测“删除”键?

As people were having problems with Swift, I figured a small, complete example in both Objective C and Swift might be a good answer. 由于人们遇到了Swift的问题,我在Objective C和Swift中找到了一个小而完整的例子可能是一个很好的答案。

Note that Swift doesn't have a \\b escape character for backspace, so you need to use a simple Unicode scalar value escape sequence of \\u{8}\u003c/code> . 请注意,Swift没有用于退格的\\b转义字符,因此您需要使用\\u{8}\u003c/code>的简单Unicode标量值转义序列。 This maps to the same old-school ASCII control character number 8 ("control-H", or ^H in caret notation ) for backspace as \\b does in Objective C. 对于退格键,这将映射到相同的旧学校ASCII控制字符编号8(“control-H”或插入符号中的 ^ H),如目标C中的\\b

Here's an Objective C view controller implementation that catches backspaces: 这是一个捕获退格的Objective C视图控制器实现:

#import "ViewController.h"

@implementation ViewController

// The View Controller must be able to become a first responder to register
// key presses.
- (BOOL)canBecomeFirstResponder {
    return YES;
}

- (NSArray *)keyCommands {
    return @[
        [UIKeyCommand keyCommandWithInput:@"\b" modifierFlags:0 action:@selector(backspacePressed)]
    ];
}

- (void)backspacePressed {
    NSLog(@"Backspace key was pressed");
}

@end

And here's the equivalent view controller in Swift: 这是Swift中的等效视图控制器:

import UIKit

class ViewController: UIViewController {

    override var canBecomeFirstResponder: Bool {
        return true;
    }

    override var keyCommands: [UIKeyCommand]? {
        return [
            UIKeyCommand(input: "\u{8}", modifierFlags: [], action: #selector(backspacePressed))
        ]
    }

    @objc func backspacePressed() {
        NSLog("Backspace key was pressed")
    }

}

很简单 - 需要查找退格字符“\\ b”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM