我有一个基本上是无线键盘的蓝牙脚踏开关。一个踏板发送上箭头键,另一个踏板发送下箭头键。我希望能够在踩下其中一个踏板时在iPad应用程序中执行自己的代码。踏板制造商告诉我,我应该创建一个UITextField
,并在包含的UIView中采用UIKeyInput
协议(protocol),并使用beginningOfDocument
和endOfDocument
方法执行我的代码。我这样做了,但是无论做什么,都不会调用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
最佳答案
您已在UIKeyInput
中采用了UIViewController
。请注意您输入的继承定义:
@interface Pedal_ProtocolViewController : UIViewController <UIKeyInput, UITextInput>{
您在这里已经说过:“这是一个实现
UIKeyInput
和UITextInput
的 View Controller 。”这两个协议(protocol)适用于UIResponder
子类,例如UIView
和子类或UIView
。 UIViewController
不是这样的类,也许不是处理文本输入的最佳类。View Controller 管理 View 。他们不是自己的看法。
您可以(而不是文本输入协议(protocol))仅使用一个隐藏的文本字段(例如您已经拥有的文本字段)。只需创建
NSObject
的子类即可为文本字段实现委托(delegate),并将其分配为文本字段的委托(delegate)。然后,在-viewDidAppear:
中,在文本字段上调用-becomeFirstResponder
以专注于该字段。您可能可以使用一些技巧来隐藏键盘。游戏和游戏支持库中通常使用此方法来显示软件键盘。它甚至可以在iOS 3.1.3和更早版本上运行(考虑到您正在为iPad开发,这对您来说不是问题)。
如果您保留该设计(在 View Controller 中处理输入),则可能需要这样做并使其起作用。
-(BOOL)canBecomeFirstResponder
{
return YES;
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self becomeFirstResponder];
}
请考虑使用
UITextField
和一个委托(delegate)来处理输入,或者在UIKeyInput
的子类中实现上述两个功能和UIView
协议(protocol)。还要注意,您不必为了获得按键就遵守
UITextInput
; UIKeyInput
就足够了。附加说明:如果您决定将
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/