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

objective-c – 采用UIKeyInput协议从蓝牙键盘获取输入,第1张

概述我有一个蓝牙脚踏开关,基本上是一个无线键盘.一个踏板发送向上箭头键,另一个发送向下箭头键.我希望能够在我的iPad应用程序中按下其中一个踏板时执行我自己的代码.踏板的制造者告诉我应该创建一个UITextField,并在包含UIView中采用UIKeyInput协议,并使用beginOfDocument和endOfDocument方法来执行我的代码.我这样做了,但不管我做什么,都不会调用UIKeyI 我有一个蓝牙脚踏开关,基本上是一个无线键盘.一个踏板发送向上箭头键,另一个发送向下箭头键.我希望能够在我的iPad应用程序中按下其中一个踏板时执行我自己的代码.踏板的制造者告诉我应该创建一个UITextFIEld,并在包含UIVIEw中采用UIKeyinput协议,并使用beginofdocument和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
解决方法 您已在UIVIEwController中采用了UIKeyinput.请注意您输入的继承定义:

@interface Pedal_ProtocolVIEwController : UIVIEwController <UIKeyinput,UITextinput>{

你在这里说过“这是一个实现UIKeyinput和UITextinput的视图控制器.”这两个协议适用于UIResponder子类,如UIVIEw和子类或UIVIEw. UIVIEwController不是一个这样的类,也许不是处理文本输入的最佳类.

VIEw控制器管理视图.他们不是自己的观点.

您可以(而不是文本输入协议)只使用隐藏的文本字段(例如您已有的字段).只需创建一个NSObject的子类,它实现文本字段的委托,并将其指定为文本字段的委托.然后,在-vIEwDIDAppear:中,在文本字段上调用-becomeFirstResponder以关注该字段.您可以使用一些黑客来隐藏键盘.

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

如果您将保留该设计(在视图控制器中处理输入),则可能需要这样做并使其工作.

-(BOol)canBecomeFirstResponder{    return YES;}-(voID)vIEwDIDAppear:(BOol)animated{    [super vIEwDIDAppear:animated];    [self becomeFirstResponder];}

请考虑使用UITextFIEld和委托来处理输入,或者在UIVIEw的子类中实现上述两个函数和UIKeyinput协议.

另请注意,您不需要遵循UITextinput来获取按键; UIKeyinput就足够了.

附加说明:如果您决定子类化UIVIEw(与我使用隐藏的UITextFIEld一起使用;我还没有尝试子类化UIVIEwController来获取键盘输入),您需要将-becomeFirstResponder添加到-awakeFromNib:

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

那就是你从一个笔尖加载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协议从蓝牙键盘获取输入全部内容,希望文章能够帮你解决objective-c – 采用UIKeyInput协议从蓝牙键盘获取输入所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1084595.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存