UIViewから継承し、 UIKeyInput * .h *に準拠するクラスがあります。
@interface UIKeyInputExampleView : UIView <UIKeyInput>{
NSMutableString *textStore;
}
@property (nonatomic, retain) NSMutableString *textStore;
@end
.m
@implementation UIKeyInputExampleView
@synthesize textStore;
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Initialization code
self.textStore = [NSMutableString string];
[self.textStore appendString:@"Touch screen to edit."];
self.backgroundColor = [UIColor whiteColor];
}
return self;
}
- (void)dealloc {
[textStore dealloc];
[super dealloc];
}
#pragma mark -
#pragma mark Respond to touch and become first responder.
- (BOOL)canBecomeFirstResponder { return YES; }
-(void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event {
[self becomeFirstResponder];
}
#pragma mark -
#pragma mark Drawing
- (void)drawRect:(CGRect)rect {
CGRect rectForText = CGRectInset(rect, 20.0, 20.0);
UIRectFrame(rect);
[self.textStore drawInRect:rectForText withFont:[UIFont fontWithName:@"Helvetica" size:24.0f]];
}
#pragma mark -
#pragma mark UIKeyInput Protocol Methods
- (BOOL)hasText {
if (textStore.length > 0) {
return YES;
}
return NO;
}
- (void)insertText:(NSString *)theText {
NSLog(@"Text have just enter:%@ length=%d ascii=%d",theText,theText.length,[theText characterAtIndex:0]);
if ([theText isEqualToString:@"\n"]) {
NSLog(@"Enter have just pressed!");
[self resignFirstResponder];
}
self.textStore = (NSMutableString*)theText;
[self setNeedsDisplay];
}
- (void)deleteBackward {
self.textStore = (NSMutableString*)@"delete";
[self setNeedsDisplay];
}
@end
英語またはベトナム語のキーボードを使用すると、すべてが正しくなります。しかし、日本語キーボードを使用すると、イベントは呼び出されず、例外もスローされません。私はいくつかのプロトコルに準拠していないと思います
手伝って頂けますか?