创建自己键盘的代码示例



我已经四处寻找创建自定义键盘的样本,但似乎没有什么帮助。所以请给我提供一个代码样本来创建我的自定义键盘

请帮我一下

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文件。用"buttontap"方法连接每个按钮。为每个按钮分配适当的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); 
}

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

相关内容

最新更新