Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Custom keyboard in UITextField

Oursky Limited
November 01, 2011
560

Custom keyboard in UITextField

Create custom keyboard in UITextField and small tips to pass custom object between TTTableViewController in Three20

Oursky Limited

November 01, 2011
Tweet

Transcript

  1. 1. Create Number Pad as UIViewController #import <UIKit/UIKit.h> @protocol NumberPadInputViewControllerDelegate

    - (void)numberKeyPressed:(NSInteger)value; - (void)backKeyPressed; @end @interface NumberPadInputViewController : UIViewController{ id<NumberPadInputViewControllerDelegate> _delegate; } @property (nonatomic, assign) id<NumberPadInputViewControllerDelegate> delegate; - (IBAction)numberKeyPressed:(id)sender; - (IBAction)backKeyPressed:(id)sender; @end •Implement the keyboard in xib Tuesday, 1 November, 11
  2. 2. Create the Number Field #import <Foundation/Foundation.h> #import "NumberPadInputViewController.h" @interface

    NumberTextField : UITextField<NumberPadInputViewControllerDelegate>{ NumberPadInputViewController *_numberPadInputView; } @property (nonatomic, retain) NumberPadInputViewController *numberPadInputView; @end - (UIView *)inputView { if (_numberPadInputView == nil) { self.numberPadInputView = [[[NumberPadInputViewController alloc] initWithNibName:@"NumberPadInputViewController" bundle:[NSBundle mainBundle]] autorelease]; _numberPadInputView.delegate = self; } return _numberPadInputView.view; } •Return the view of keypad UIViewController - (void)numberKeyPressed:(NSInteger)value{ self.text = [NSString stringWithFormat:@"%@%d", self.text, value]; } - (void)backKeyPressed{ if (self.text) { if ([self.text length]) { self.text = [self.text substringToIndex:[self.text length] - 1]; } } } •Implement keypad UIViewControllerDelegate IBAction Tuesday, 1 November, 11
  3. Don’t want a full width keyboard? •The keyboard are displayed

    in full width even the view return in inputView are not full width Work around ... •Add the keyboard view manually when the UITextField is begin to edit •Cannot be done by extend the UITextField, need to implement in UITextFieldDelegate •Disable the current keyboard • - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { return NO; } - (UIView *)inputView { return [[[UIView alloc] init] autorelease]; } Instead of Tuesday, 1 November, 11
  4. Another note URL Navigation in Three20 TTURLAction *action = [[

    [TTURLAction actionWithURLPath:@"tt://description"] applyQuery:[NSDictionary dictionaryWithObjectsAndKeys: field.caption, @"description", field.text, @"title", nil]] applyAnimated:YES]; [[TTNavigator navigator] openURLAction:action]; // Destination view controller - (id) initWithNavigatorURL:(NSURL*)URL query:(NSDictionary*)query { if (self == [self init]) { NSString *title = [query objectForKey:@"title"]; NSString *description = [query objectForKey:@"description"]; self.dataSource = [TTSectionedDataSource dataSourceWithObjects: @"", [TTTableTextItem itemWithText:title], [TTTableTextItem itemWithText:description], nil]; self.title = title; } return self; } Tuesday, 1 November, 11
  5. Another note URL Navigation in Three20 •Move it into TTTableViewDelegate

    or TTTableViewVarHeightDelegate - (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath { id<TTTableViewDataSource> dataSource = (id<TTTableViewDataSource>)tableView.dataSource; id object = [dataSource tableView:tableView objectForRowAtIndexPath:indexPath]; if ([object isKindOfClass:[TTTableLinkedItem class]]) { TTTableLinkedItem* item = object; if (item.URL && [_controller shouldOpenURL:item.URL]) { if ([object respondsToSelector:@selector(modelData)]) { // pass the modelData in custom table item to next view TTURLAction *action = [[ [TTURLAction actionWithURLPath:item.URL] applyQuery:[NSDictionary dictionaryWithObjectsAndKeys: [object performSelector:@selector(modelData)], @"modelData", nil]] applyAnimated:YES]; [[TTBaseNavigator navigatorForView:tableView] openURLAction:action]; }else{ TTOpenURLFromView(item.URL, tableView); } } else if (item.delegate && item.selector) { [item.delegate performSelector:item.selector withObject:object]; } ......... } Tuesday, 1 November, 11
  6. Another note URL Navigation in Three20 •In the TTTableViewController -

    (id<UITableViewDelegate>)createDelegate { return [[[IQueueTableViewDelegate alloc] initWithController:self] autorelease]; } Finally •You can pass the custom object between TTTableViewController easily by putting the object into table item Tuesday, 1 November, 11