Click here to Skip to main content
Click here to Skip to main content
Technical Blog

UITextField Custom Input Accessory View Class

, 29 Jan 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
UITextField custom input accessory view class.

Sometimes we need an accessory view of uitextfield to perform some action like hide the keyboard. To solve this I created a class which will add a toolbar on top of the keyboard holding a done button. If the done button is pressed then the keyboard will be hidden.

Here is the header file:

//
// RCDinputAccessoryView.h
// textInputViewDemo
//
// Created by Razib Chandra Deb on 1/28/13.
// Copyright (c) 2013 RazibDeb. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface RCDinputAccessoryView : UIView
{
UITextField * myTextField;
}
@property (nonatomic, retain)UIToolbar *inputToolBar;
@property (nonatomic,retain)UIBarButtonItem *doneButton;
- (id)initWithTextField:(UITextField*)textField;
@end

Here is the .m file:

//
// RCDinputAccessoryView.m
// textInputViewDemo
//
// Created by Razib Chandra Deb on 1/28/13.
// Copyright (c) 2013 RazibDeb. All rights reserved.
//
#import “RCDinputAccessoryView.h”
@implementation RCDinputAccessoryView
@synthesize inputToolBar;
@synthesize doneButton;
- (id)initWithTextField:(UITextField*)textField
{
self = [super initWithFrame:CGRectMake(0, 250, 320, 40)];
if (self) {
myTextField=textField;
inputToolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
doneButton= [[[UIBarButtonItem alloc]
initWithTitle:@”DONE”
style:UIBarButtonItemStyleDone
target:self
action:@selector(performdone:)]
autorelease];
inputToolBar.barStyle=UIBarStyleDefault;
UIBarButtonItem* flexSpace = [[UIBarButtonItem alloc] 
  initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[inputToolBar setItems:[NSArray arrayWithObjects:flexSpace,doneButton,Nil] animated:NO];
[self addSubview:inputToolBar];
}
return self;
}
-(IBAction)performdone:(id)sender
{
NSLog(@”done clicked”);
[myTextField resignFirstResponder];
}
@end

Usages:

RCDinputAccessoryView *myInputAccessoryView=[[RCDinputAccessoryView alloc]initWithTextField: self.myTextField];
[self.myTextField setInputAccessoryView:myInputAccessoryView];

To download the demo project click here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Razib Chandra Deb
Student
Bangladesh Bangladesh
No Biography provided
Follow on   Twitter

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141220.1 | Last Updated 29 Jan 2013
Article Copyright 2013 by Razib Chandra Deb
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid