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 create a class which will add a toolbar on top of the keyboard holding a done button. If done button is pressed then keyboard will be hide.

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

Advertisements

About Razib Chandra Deb

I completed my BSc in CSE from CSE Department of KUET. Currently I am working as a Software Engineer in Samsung R&D Institute Bangladesh(SRBD). I am interested in image processing.
This entry was posted in CodeProject, iPhone and tagged , , , , . Bookmark the permalink.

One Response to UITextField Custom Input Accessory View Class

  1. Anonymous says:

    Good article. I definitely appreciate this website. Keep writing!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s