代码示例创建自己的键盘

时间:2011-11-30 09:48:14

标签: iphone ios ios4

我一直在寻找创建自定义键盘的示例,但似乎没有任何帮助。所以请提供一个代码示例来创建我的自定义键盘

请帮帮我

2 个答案:

答案 0 :(得分:4)

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]];
    }
}

现在 - 使用您想要的任何视图制作您自己的.xib文件。使用“buttonTapped”方法连接每个按钮。将适当的ASCII码分配为每个按钮的标签。我正在使用这个解决方案,一切正常。

使用方法: 您正在创建视图控制器作为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); 

}

答案 1 :(得分:1)

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