问题
I have searched around for making a sample on creating my custom keyboard but nothing seemed to help. So Please provide me a code sample to create my custom keyboard
please help me out
回答1:
YourKeyboard.h :
#import <UIKit/UIKit.h>
@protocol YourKeyboardControllerDelegate
-(void) buttonTapped: (int) ASCIICode;
@end
@interface YourKeyboardViewController : UIViewController
{
id<YourKeyboardControllerDelegate> delegate;
}
@property (nonatomic,weak) id<YourKeyboardControllerDelegate> delegate;
@end
YourKeyboard.m :
#import "YourKeyboardViewController.h"
@implementation YourKeyboardViewController
@synthesize delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (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
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return NO;
}
- (IBAction)buttonTapped:(id)sender {
if (delegate != nil)
{
[delegate buttonTapped:[sender tag]];
}
}
Now - make your own .xib file with any view you want. Connect every button with "buttonTapped" method. Assign proper ASCII code as a tag for each button. I am using this solution and everything works fine.
Usage: You are creating view controller as a YourKeyboardControllerDelegate.
@interface MainViewController : UIViewController <YourKeyboardControllerDelegate>
{
YourKeyboardViewController *keyboard
}
and in .m file
- (void)viewDidLoad
{
keyboard = [[YourKeyboardViewController alloc]initWithNibName:@"nib file for keyboard" bundle:[NSBundle mainBundle]];
keyboard.delegate = self;
// connecting new keyboard to textfield
someTextField.inputView = keyboard.view;
}
-(void) buttonTapped: (int)ASCIICode;
{
NSLog(@"you tapped button with %d", ASCIICode);
}
回答2:
Here is some code samples for creating custom keyboards. It seems like a nice solution: http://www.iphonedevsdk.com/forum/iphone-sdk-tutorials/7350-adding-subviews-custimize-keyboard.html
来源:https://stackoverflow.com/questions/8323928/code-sample-to-create-own-keyboard