- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个基本上是无线键盘的蓝牙脚踏开关。一个踏板发送向上箭头键,另一个发送向下箭头键。当按下其中一个踏板时,我希望能够在我的 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
最佳答案
您在 UIViewController
中采用了 UIKeyInput
。请注意您输入的继承定义:
@interface Pedal_ProtocolViewController : UIViewController <UIKeyInput, UITextInput>{
您在这里说过“这是一个实现了 UIKeyInput
和 UITextInput
的 View Controller 。”这两个协议(protocol)适用于UIResponder
子类,如UIView
和子类或UIView
。 UIViewController
是
不是这样的类
可能不是处理文本输入的最佳类。
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)>.
另请注意,您不必为了获得按键而遵守 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/
我可以看到有状态的协议(protocol)可以减少像 cookie 这样的“模拟状态”。 但是测试变得更加难以确保您的实现正确并重新连接,并且 session 继续可能很难处理。 始终使用无状态协议(
我正在尝试为我的下一个分布式应用程序找到合适的协议(protocol)中间件。在过去的几天里,我找到了几个规范,想知道我是否错过了一个重要的规范?它应该是二进制协议(protocol),支持 RPC,
我正在做一个研究生院软件工程项目,我正在寻找管理 ATM 和银行网络之间通信的协议(protocol)。 我已经在谷歌上搜索了很长一段时间,虽然我找到了各种有关 ATM 的有趣信息,但我惊讶地发现似乎
我正在开发一个 ECG 模块,它以字节为单位给出数据。有一个关于它的协议(protocol)文档解释了如何构建从模块中出来的数据包。我想解码该数据。我很困惑 Protocol Buffer 是否会对此
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 3年前关闭。 Improve this qu
我需要在我的程序中包含基本的文件发送和文件接收例程,并且需要通过 ZMODEM 协议(protocol)。问题是我无法理解规范。 供引用,here is the specification . 规范没
我最近听到这个术语来描述 Google 的新环聊协议(protocol)和 Whisper System 的新 encrypted texting app . The new TextSecure p
如何检查某个对象是否符合协议(protocol)? 我试过这种方式,但出现错误: if lCell.conformsToProtocol(ContentProtocol) { } 最佳
在应用程序中,我们有两种类型的贴纸,字符串和位图。每个贴纸包都可以包含两种类型。这就是我声明模型的方式: // Mark: - Models protocol Sticker: Codable { }
这个问题在这里已经有了答案: Why can't a get-only property requirement in a protocol be satisfied by a property w
我有以下快速代码: protocol Animal { var name: String { get } } struct Bird: Animal { var name: String
我在遵循继承树的几个类中分配协议(protocol)。像这样: 头等舱 @protocol LevelOne - (void) functionA @end @interface BaseClass
我们之前使用的是 fix,但客户说使用 OUCH 进行交易,因为这样速度更快。我在互联网上查了一下,消息看起来很相似。它如何获得速度优势。请给我一些示例消息 最佳答案 基本上,FIX 消息以文本格式传
在我的 swift 项目中,我有一个使用协议(protocol)继承的案例,如下所示 protocol A : class{ } protocol B : A{ } 接下来我要实现的目标是声明另一个具
我想根据这两种协议(protocol)的一般特征(例如开销(数据包)、安全性、信息建模和可靠性)来比较 OPC UA 和 MQTT。我在哪里可以找到每个协议(protocol)的开销和其他特性的一些示
本质上,我的最终目标是拥有一个协议(protocol) Log,它强制所有符合它的对象都有一个符合另一个协议(protocol) [LogEvent] 的对象数组. 但是,符合Log的类需要有特定类型
我正在尝试为基于左操作数和右操作数标识的协议(protocol)实现 Equatable 协议(protocol)。换句话说:我如何为一个协议(protocol)实现 Equatable 协议(pro
问题不在于编程。 我正在使用一台旧机器,微软停止了这些机器的补丁。 有没有人针对攻击者已知的使用端口 445 的 SMB 协议(protocol)漏洞的解决方案? 任何棘手的解决方案? 换句话说,我想
在我们的业务中,我们需要记录到达我们服务器的每个请求/响应。 目前,我们使用 xml 作为标准实现。 如果我们需要调试/跟踪某些错误,则使用日志文件。 如果我们切换到 Protocol Buffer
你推荐什么协议(protocol)定义? 我评估了 Google 的 Protocol Buffer ,但它不允许我控制正在构建的数据包中字段的位置。我认为 Thrift 也是如此。我的要求是: 指定
我是一名优秀的程序员,十分优秀!