Quantcast
Channel: Razib Chandra Deb » UITextField
Viewing all articles
Browse latest Browse all 2

UITextField Custom Input Accessory View Class

$
0
0

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



Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles



Latest Images