gpt4 book ai didi

objective-c - 采用 UIKeyInput 协议(protocol)从蓝牙键盘获取输入

转载 作者:可可西里 更新时间:2023-11-01 03:08:40 26 4
gpt4 key购买 nike

我有一个基本上是无线键盘的蓝牙脚踏开关。一个踏板发送向上箭头键,另一个发送向下箭头键。当按下其中一个踏板时,我希望能够在我的 iPad 应用程序中执行我自己的代码。踏板的制造者告诉我我应该创建一个 UITextField,并在包含的 UIView 中采用 UIKeyInput 协议(protocol)并使用 beginningOfDocumentendOfDocument 方法来执行我的代码。我这样做了,但无论我做什么,都没有调用 UIKeyInput 或 UITextInput 方法。任何人都可以引导我完成这个,或者指导我学习类似的教程吗?有更简单的方法吗?

感谢您的帮助。

这是我的.h:

#import <UIKit/UIKit.h>

@interface Pedal_ProtocolViewController : UIViewController <UIKeyInput, UITextInput>{
UITextField *myTextField;
}
@property (nonatomic, retain) IBOutlet UITextField *myTextField;
@end

这是我的.m:

#import "Pedal_ProtocolViewController.h"

@implementation Pedal_ProtocolViewController

@synthesize myTextField;

- (void)dealloc
{
[super dealloc];
}

- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
[myTextField canBecomeFirstResponder];
[myTextField becomeFirstResponder];
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}

#pragma mark -
#pragma mark UIKeyInput Protocol Methods

- (BOOL)hasText {
return NO;
}

- (void)insertText:(NSString *)theText {
}

- (void)deleteBackward {
}

- (BOOL)canBecomeFirstResponder {
return YES;
}

#pragma mark -
#pragma mark UITextInput Protocol Methods

- (NSString *)textInRange:(UITextRange *)range {
return @"";
}
- (void)replaceRange:(UITextRange *)range withText:(NSString *)text {
}
- (void) setSelectedTextRange: (UITextRange *) range {
}
- (UITextRange *) markedTextRange {
return nil;
}
- (NSDictionary *) markedTextStyle {
return nil;
}
- (void) setMarkedTextStyle: (NSDictionary *) style {
}
- (void)setMarkedText:(NSString *)markedText selectedRange:(NSRange)selectedRange {
}
- (void) unmarkText {
}
- (UITextPosition *) endOfDocument {
//DOWN KEY

NSLog(@"Down");
return nil;
}
- (UITextPosition *) beginningOfDocument {
//UP KEY

NSLog(@"UP");
return nil;
}
- (UITextRange *)textRangeFromPosition:(UITextPosition *)fromPosition toPosition:(UITextPosition *)toPosition{
return nil;
}
- (UITextPosition *)positionFromPosition:(UITextPosition *)position offset:(NSInteger)offset{
return nil;
}
- (UITextPosition *)positionFromPosition:(UITextPosition *)position inDirection:(UITextLayoutDirection)direction offset:(NSInteger)offset {
return nil;
}
- (NSComparisonResult) comparePosition: (UITextPosition *)position toPosition: (UITextPosition *)other {
return NSOrderedSame;
}
- (NSInteger) offsetFromPosition: (UITextPosition *)from toPosition: (UITextPosition *)toPosition {
return 0;
}
- (void) setInputDelegate: (id <UITextInputDelegate>) delegate {
}
- (id <UITextInputDelegate>) inputDelegate {
return nil;
}
- (id <UITextInputTokenizer>) tokenizer {
return nil;
}
- (UITextPosition *)positionWithinRange:(UITextRange *)range farthestInDirection:(UITextLayoutDirection)direction {
return nil;
}
- (UITextRange *) characterRangeByExtendingPosition: (UITextPosition *) position inDirection: (UITextLayoutDirection) direction {
return nil;
}
- (UITextWritingDirection) baseWritingDirectionForPosition: (UITextPosition *)position inDirection: (UITextStorageDirection)direction {
return 0;
}
- (void) setBaseWritingDirection: (UITextWritingDirection)writingDirection forRange:(UITextRange *)range {
}
- (CGRect) firstRectForRange: (UITextRange *) range {
return CGRectZero;
}
- (CGRect) caretRectForPosition: (UITextPosition *) position {
return CGRectZero;
}
- (UITextPosition *) closestPositionToPoint: (CGPoint)point {
return nil;
}
- (UITextPosition *) closestPositionToPoint: (CGPoint)point withinRange: (UITextRange *) range {
return nil;
}
- (UITextRange *) characterRangeAtPoint: (CGPoint)point {
return nil;
}
- (UITextRange *) selectedTextRange {
return [[UITextRange alloc]init];
}

@end

最佳答案

您在 UIViewController 中采用了 UIKeyInput。请注意您输入的继承定义:

@interface Pedal_ProtocolViewController : UIViewController <UIKeyInput, UITextInput>{

您在这里说过“这是一个实现了 UIKeyInputUITextInput 的 View Controller 。”这两个协议(protocol)适用于UIResponder子类,如UIView和子类或UIViewUIViewController 不是这样的类 可能不是处理文本输入的最佳类。

View Controller 管理 View 。它们本身不是 View 。

您可以(而不是文本输入协议(protocol))只使用一个隐藏的文本字段(例如您已有的文本字段)。只需创建一个 NSObject 的子类来实现文本字段的委托(delegate),并将其指定为文本字段的委托(delegate)。然后,在 -viewDidAppear: 中,在文本字段上调用 ​​-becomeFirstResponder 以聚焦该字段。您或许可以使用一些 hack 来隐藏键盘。

这种方法通常用于游戏和游戏支持库中以显示软件键盘。它甚至适用于 iOS 3.1.3 及更早版本(考虑到您正在为 iPad 开发,这对您来说不是问题)。


如果您将保留该设计(在 View Controller 中处理输入),那么这可能是必需的并使其工作。

-(BOOL)canBecomeFirstResponder
{
return YES;
}

-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];

[self becomeFirstResponder];
}

请考虑使用 UITextField 和委托(delegate)来处理输入,或者在 UIView 的子类中实现上述两个函数和 UIKeyInput 协议(protocol)>.

另请注意,您不必为了获得按键而遵守 UITextInputUIKeyInput 就够了。


附加说明:如果您决定子类化 UIView(这与使用隐藏的 UITextField 一起,是我所做的;我还没有尝试子类化 UIViewController 获取键盘输入),您需要将 -becomeFirstResponder 添加到 -awakeFromNib 中:

-(void)awakeFromNib
{
[super awakeFromNib];
[self becomeFirstResponder];
}

如果您从 nib 加载 UIViewController(以及 UIView)。不这样做?尝试在 -initWithFrame: 中添加:

-(id)initWithFrame:(CGFrame)frame
{
self = [super initWithFrame:frame];
if(!self)
return nil;

[self becomeFirstResponder];
return self;
}

或者,在 UIViewController 的 viewDidLoad 中:

    // ...
[self.view becomeFirstResponder];
// ...

显然有很多方法可以做到这一点。 ;)

关于objective-c - 采用 UIKeyInput 协议(protocol)从蓝牙键盘获取输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7410854/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com