In IOS 8 Extensions are added and it’s a wonderful feature that IOS user are waiting from long time. In last post we covered basics of creating keyboard extension in IOS 8 SDK, you can read it from Here
File selection interface Xcode Open, MYKeyboardView.xib file and select view Now on right side pane set Size property to Freeform Set view size to 320*216 standard size of iPhone keyboard Drag three buttons on the view and set there tittle to A, B, C
#import @interface KeyboardViewController : UIInputViewController
{
} -(IBAction)tapOnButton:(id)sender; @end Open KeyboardViewController.m file and write down implementation for our IBAction Created by us. Wait a minute now question arises how would IOS system knows that what we want to type in the fields.For this we have textDocumentProxy property UIInputViewController class and our KeyboardViewController is inherited from UIInputViewController. -(IBAction)tapOnButton🙁id)sender { UIButton *button = (UIButton *)sender; if ([button.titleLabel.text isEqualToString:@“A”])
{ [self.textDocumentProxy insertText:@”A”]; } else if (titleLa[button.bel.text isEqualToString:@“B”])
{ [self.textDocumentProxy insertText:@”B”]; } else if (titleLa[button.bel.text isEqualToString:@“C”])
{
[self.textDocumentProxy insertText:@”C”]; } } Open MYKeyboardView.xib and give our IBAction to three buttons that we dragged to our view earlier. Before doing this we have to set class name or owner name of our .xib file. Select File owner and set class name to KeyboardViewController.
Now we have to load our view when keyboard loads, open KeyboardViewController.m and comment all code from ViewDidLoad method. Add below code to it, this will add our view when keyboard appears UIView *customKeyBoardview = [[[NSBundle mainBundle]loadNibNamed:@”MYKeyboardView” owner:self options:nil] objectAtIndex:0]; [self.view addSubview:customKeyBoardview]; Run your app, you will see your keyboard with three buttons, if not then check your settings app may be you forgot to enable it. If keyboard does not appear in simulator then try reset it. Image shown below of CustomKeyboard running on simulator
IBOutlet UIButton *globeButton; Open KeyboardViewController.m and in viewDidLoad add below line of code [globeButton addTarget:self action:@selector(advanceToNextInputMode) forControlEvents:UIControlEventTouchUpInside]; Complete code will look like this – (void)viewDidLoad { [super viewDidLoad]; UIView *customKeyBoardview = [[[NSBundle mainBundle]loadNibNamed:@”MYKeyboardView” owner:self options:nil] objectAtIndex:0]; [self.view addSubview:customKeyBoardview]; [globeButton addTarget:self action:@selector(advanceToNextInputMode) forControlEvents:UIControlEventTouchUpInside]; } Run your app now, and tap on globe button, it will change keyboard to next keyboard i.e. default keyboard. Source code:Download code from here Hope you enjoyed the post. Comments are welcomed 🙂
|