fre*_*rdo 8 subclass objective-c ipad ios
我有一个蓝牙脚踏开关,基本上是一个无线键盘.一个踏板发送向上箭头键,另一个发送向下箭头键.我希望能够在我的iPad应用程序中按下其中一个踏板时执行我自己的代码.踏板的制造者告诉我应该创建一个UITextField,并UIKeyInput在包含UIView中采用协议并使用beginningOfDocument和endOfDocument方法来执行我的代码.我这样做了,但不管我做什么,都不会调用UIKeyInput或UITextInput方法.任何人都可以引导我完成这个,或者指导我学习与此相似的教程吗?有更简单的方法吗?
谢谢你的帮助.
这是我的.h:
#import <UIKit/UIKit.h>
@interface Pedal_ProtocolViewController : UIViewController <UIKeyInput, UITextInput>{
UITextField *myTextField;
}
@property (nonatomic, retain) IBOutlet UITextField *myTextField;
@end
Run Code Online (Sandbox Code Playgroud)
这是我的.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
Run Code Online (Sandbox Code Playgroud)
你已经采用UIKeyInput了UIViewController.请注意您输入的继承定义:
@interface Pedal_ProtocolViewController : UIViewController <UIKeyInput, UITextInput>{
Run Code Online (Sandbox Code Playgroud)
你在这里说过"这是一个实现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];
}
Run Code Online (Sandbox Code Playgroud)
请考虑使用UITextField和委托来处理输入,或者UIKeyInput在子类中实现上述两个函数和协议UIView.
另请注意,您无需遵守UITextInput以获得按键; UIKeyInput足够.
附加说明:如果你决定子类UIView(这与我使用的隐藏UITextField,是我做的;我没有尝试子类化UIViewController来获取键盘输入),你会想要添加-becomeFirstResponder到-awakeFromNib:
-(void)awakeFromNib
{
[super awakeFromNib];
[self becomeFirstResponder];
}
Run Code Online (Sandbox Code Playgroud)
那就是你从笔尖加载UIViewController(以及因此UIView).不这样做?尝试添加-initWithFrame::
-(id)initWithFrame:(CGFrame)frame
{
self = [super initWithFrame:frame];
if(!self)
return nil;
[self becomeFirstResponder];
return self;
}
Run Code Online (Sandbox Code Playgroud)
或者,在UIViewController中viewDidLoad:
// ...
[self.view becomeFirstResponder];
// ...
Run Code Online (Sandbox Code Playgroud)
显然有很多方法可以做到这一点.;)
| 归档时间: |
|
| 查看次数: |
10884 次 |
| 最近记录: |