代码示例创build自己的键盘

我search了四处创造我的自定义键盘样品,但似乎没有任何帮助。 所以请给我一个代码示例来创build我的自定义键盘

请帮助我

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. // eg self.myOutlet = nil; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return NO; } - (IBAction)buttonTapped:(id)sender { if (delegate != nil) { [delegate buttonTapped:[sender tag]]; } } 

现在 – 用你想要的任何视图制作你自己的.xib文件。 用“buttonTapped”方法连接每个button。 为每个button分配正确的ASCII码作为标签。 我正在使用这个解决scheme,一切工作正常。

用法:您正在创build视图控制器作为YourKeyboardControllerDelegate。

 @interface MainViewController : UIViewController <YourKeyboardControllerDelegate> { YourKeyboardViewController *keyboard } 

和.m文件中

 - (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); } 

以下是一些用于创build自定义键盘的代码示例。 这似乎是一个不错的解决scheme: http : //www.iphonedevsdk.com/forum/iphone-sdk-tutorials/7350-adding-subviews-custimize-keyboard.html