I'm trying to add a "done" button to the UIKeyboadnumpad, but with no success.
What's wrong in my code?
the keyboard don't have the done button
#implementation DemoViewController
- (void)loadView {
self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 26)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.keyboardType = UIKeyboardTypeNumberPad;
textField.returnKeyType = UIReturnKeyDone;
textField.textAlignment = UITextAlignmentLeft;
textField.text = #"12345";
[self.view addSubview:textField];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
}
- (void)keyboardWillShow:(NSNotification *)note {
// create custom button
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 163, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
[doneButton setImage:[UIImage imageNamed:#"DoneUp.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:#"DoneDown.png"] forState:UIControlStateHighlighted];
[doneButton addTarget:self action:#selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
// locate keyboard view
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
UIView* keyboard;
for(int i=0; i<[tempWindow.subviews count]; i++) {
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard view found; add the custom button to it
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
if([[keyboard description] hasPrefix:#"<UIPeripheralHost"] == YES)
[keyboard addSubview:doneButton];
} else {
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES)
[keyboard addSubview:doneButton];
}
}
}
- (void)doneButton:(id)sender {
NSLog(#"Input: %#", textField.text);
[textField resignFirstResponder];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[textField release];
[super dealloc];
}
#end
Another solution. Perfect if there are other non-number pad text fields on the screen.
- (void)viewDidLoad
{
[super viewDidLoad];
UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
numberToolbar.barStyle = UIBarStyleBlackTranslucent;
numberToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(cancelNumberPad)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:#"Apply" style:UIBarButtonItemStyleDone target:self action:#selector(doneWithNumberPad)],
nil];
[numberToolbar sizeToFit];
numberTextField.inputAccessoryView = numberToolbar;
}
-(void)cancelNumberPad{
[numberTextField resignFirstResponder];
numberTextField.text = #"";
}
-(void)doneWithNumberPad{
NSString *numberFromTheKeyboard = numberTextField.text;
[numberTextField resignFirstResponder];
}
I needed the phone pad (with the +*#) and not the number pad, do I didn't even had the empty button in the corner.
Write your add button's code in
- (void)keyboardDidShow:(NSNotification *)note
instead of
- (void)keyboardWillShow:(NSNotification *)note
For this implement UIKeyboardDidShowNotification notification like :
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification object:nil];
I think UIView* keyboard; is not getting the keyboard's view as keyboard is not displayed yet, it will display !!!
Some of the tutorials are incomplete or are much older. This tutorial works from IOS 4.3 onwards and I checked it. Save the two image graphics, and paste in the code. There is very little to change. Here is the link.
ps. I am not affiliated in any way with this article, but found it to be complete.
http://www.neoos.ch/blog/37-uikeyboardtypenumberpad-and-the-missing-return-key
To make it short: take a screenshot, cut out the whole backspace key, flip it horizotally, clear its backspace symbol in Photoshop and overlay it with the text that we want on our return key. We’ve chosen to label it DONE.
Now we have the image for our custom button’s UIControlStateNormal.
Repeat the whole procedure (with a touched backspace key when taking the screenshot) to get a second image for our button’s UIControlStateHighlighted.
Here’s the result: <missing image>
Now back to coding:
First we need to know when the number pad is going to slide up on the screen so that we can plug our custom button before that happens.
Luckily there’s a notification for exactly that purpose and registering for it is as easy as:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
Don't forget to remove the observer from the notification center in the appropriate place once you're done with the whole thing:
[[NSNotificationCenter defaultCenter] removeObserver:self];
Now we’re getting to the heart of it. All we have to do in the keyboardWillShow method is to locate the keyboard view and add our button to it.
The keyboard view is part of a second UIWindow of our application as others have already figured out (see this thread).
So we take a reference to that window (it will be the second window in most cases, so objectAtIndex:1 in the code below is fine), traverse its view hierarchy until we find the keyboard and add the button to its lower left:
- (void)keyboardWillShow:(NSNotification *)note {
// create custom button
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 163, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
[doneButton setImage:[UIImage imageNamed:#"DoneUp.png"]
forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:#"DoneDown.png"]
forState:UIControlStateHighlighted];
[doneButton addTarget:self action:#selector(doneButton:)
forControlEvents:UIControlEventTouchUpInside];
// locate keyboard view
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows]
objectAtIndex:1];
UIView* keyboard;
for(int i=0; i<[tempWindow.subviews count]; i++) {
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard view found; add the custom button to it
if([[keyboard description] hasPrefix:#"UIKeyboard"] == YES)
[keyboard addSubview:doneButton];
}
}
Voilà, that’s it!
The empty space for our button starts at coordinate (0, 163) and has the dimensions (106, 53).
The doneButton method has to be written now of course, but that’s not hard any more. Just make sure that you call resignFirstResponder on the text field that is being edited to have the keyboard slide down.
We’re “DONE”.
Related
I have a little problem with the notific of keyboard disappear, I add the observer:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardDidDisappear:)
name:UIKeyboardWillHideNotification
object:nil];
The add the method:
- (void) keyboardDidDisappear:(NSNotification *)notification {
NSLog(#"disappear");
}
In my application I have some button, on click it show two uitextfield with this method:
- (void) showFieldStoryView:(id)sender {
if (storyContentView.hidden == NO) {
UIButton *button = (UIButton *)sender;
buttonTag = [button tag];
int indexArray = buttonTag - 1;
NSMutableDictionary *dict = [arrayPunteggi objectAtIndex:indexArray];
[fieldUno removeFromSuperview];
[fieldDue removeFromSuperview];
[fieldUno setHidden:YES];
[fieldDue setHidden:YES];
[storyContentView addSubview:fieldUno];
[storyContentView addSubview:fieldDue];
[fieldUnoAccusi removeFromSuperview];
[fieldDueAccusi removeFromSuperview];
[fieldUnoAccusi setHidden:YES];
[fieldDueAccusi setHidden:YES];
[labelPunti removeFromSuperview];
[labelAccusi removeFromSuperview];
[labelPunti setHidden:YES];
[labelAccusi setHidden:YES];
[storyContentView addSubview:labelPunti];
[storyContentView addSubview:labelAccusi];
if (fieldUno.isFirstResponder) {
point = CGPointMake(0,button.frame.origin.y);
}
else {
[fieldUno becomeFirstResponder];
}
}
}
the problem is that every time I click on this button I see on console the log disappear, and the strange thins is that the keyboard is always show oh the screen, where is the problem?
I have added a view with buttons on top of keyboard using setInputAccessoryView.
Now i want functionality like when i click button from the view i want to display a pickerView on top of keyboard. Means like adding pickerView as subview of keyboard.
How can i do this?
Make a view you want to at the top of the keyboard. You can do this from xib or manually, but I think xib will be a better option.
Then make the reference to this view by doing this
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
textField.inputAccessoryView = YOURCustomView;
return YES;
}
Whenever you want to hide this or remove this just put this
textField.inputAccessoryView = nil;
self.pickerContainerView = [[UIView alloc] initWithFrame:CGRectMake(0, 194, 320, 224)];
self.pickerContainerView.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.pickerContainerView];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
[pickerToolbar sizeToFit];
self.lblPickerViewTitle = [[UILabel alloc] initWithFrame:CGRectMake(15, 7, 230, 30)];
self.lblPickerViewTitle.backgroundColor = [UIColor clearColor];
[self.lblPickerViewTitle setFont: [UIFont fontWithName:#"Arial-BoldMT" size:17]];
self.lblPickerViewTitle.textAlignment = UITextAlignmentLeft;
self.lblPickerViewTitle.textColor =[UIColor whiteColor];
[pickerToolbar addSubview:self.lblPickerViewTitle];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
[flexSpace release];
UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(closePickerView:)];
[barItems addObject:btnCancel];
[btnCancel release];
UIBarButtonItem *btnDone = [[UIBarButtonItem alloc] initWithTitle:#"ShowPicker" style:UIBarButtonItemStyleBordered target:self action:#selector(donePickerView:)];
[barItems addObject:btnDone];
[btnDone release];
[pickerToolbar setItems:barItems animated:YES];
[barItems release];
[self.pickerContainerView addSubview:pickerToolbar];
And in ShowPicker Method , write code for display UIPicker
You need to create another UIWindow first:
UIWindow *newWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0,100,320,20)];
newWindow.windowLevel = UIWindowLevelStatusBar; // this will make to place your WINDOW above the keyboard
[newWindow makeKeyAndVisible]; // show the new window
// [newWindow addSubview:yourView];
like add button on keyboard you can also add view like bellow...
here you find window of keyboard and then set frame of button and also yourView and then add to keyboard
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
return YES;
}
- (void)keyboardDidShow:(NSNotification *)note {
UIButton *returnBtn = [UIButton buttonWithType:UIButtonTypeCustom];
returnBtn.frame = CGRectMake(0,-25,320,25);
returnBtn.adjustsImageWhenHighlighted = NO;
returnBtn.backgroundColor=[UIColor darkGrayColor];
returnBtn.titleLabel.textColor=[UIColor whiteColor];
[returnBtn setBackgroundImage:[UIImage imageNamed:#"keyBtn.png"] forState:UIControlStateNormal];
[returnBtn addTarget:self action:#selector(keyboardBtn:) forControlEvents:UIControlEventTouchUpInside];
// locate keyboard view
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for(int i=0; i<[tempWindow.subviews count]; i++) {
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard found, add the button
if([[keyboard description] hasPrefix:#"<UIPeripheralHost"] == YES)
// if (txtTag==5) {
[keyboard addSubview:returnBtn];
}
}
-(IBAction)keyboardBtn:(id)sender{
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for(int i=0; i<[tempWindow.subviews count]; i++) {
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard found, add the button
if([[keyboard description] hasPrefix:#"<UIPeripheralHost"] == YES)
// if (txtTag==5) {
[keyboard addSubview:yourView];// here add your view with frame
}
}
i hope this help you...
:)
From Jonas Schnelli's answer
UIWindow *newWindow = [[UIWindow alloc]initWithFrame:CGRectMake(0,100,320,20)];
newWindow.windowLevel = CGFLOAT_MAX; // this will make to place your WINDOW above the keyboard
[newWindow addSubview:yourView];
Just change UIWindowLevelStatusBar to CGFLOAT_MAX will be ok.
I have a layout where i have two text fields one is for alphabetic input and another one for numeric input. I have set different type of keyboards for these like as default for alphabetic text field and number for numeric field.
I have added a done button on number pad. Still I am getting this a issue with keyboards that is when I taped on numeric text filed first then it show done button keyboard while I taped alphabetic text filed first and then taped on numeric text field then it doesn't add done button on number pad.
How do I solve it?
I have use these code to add button on number pad.
-(void)viewWillAppear:(BOOL)animated
{
// Register for the events
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector (keyboardDidShow:) name: UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector (keyboardDidHide:) name: UIKeyboardDidHideNotification object:nil];
keyboardVisible = NO;
scroll_view.contentSize=CGSizeMake(320, 400);
}
- (void)keyboardDidShow:(NSNotification *)note {
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for (keyboard in tempWindow.subviews) {
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES)
if (numberPadShowing) {
[self addButtonToKeyboard];
return;
break;
} else {
for (UIView *v in [keyboard subviews]){
if ([v tag]==123)
[v removeFromSuperview];
}
}
}
}
-
(void) keyboardDidHide: (NSNotification *)notif
{
// Is the keyboard already shown
if (!keyboardVisible) {
return;
}
// Keyboard is no longer visible
keyboardVisible = NO;
}
-(void)doneButton
{
UIScrollView *scrollView =[[UIScrollView alloc] init];
scrollView=scroll_view;
[scrollView setContentOffset:svos animated:YES];
[myActiveTextField resignFirstResponder];
}
- (void)addButtonToKeyboard {
// create custom button
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 163, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) {
[doneButton setImage:[UIImage imageNamed:#"DoneUp3.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:#"DoneDown3.png"] forState:UIControlStateHighlighted];
} else {
[doneButton setImage:[UIImage imageNamed:#"DoneUp.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:#"DoneDown.png"] forState:UIControlStateHighlighted];
}
[doneButton addTarget:self action:#selector(doneButton) forControlEvents:UIControlEventTouchUpInside];
// locate keyboard view
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for(int i=0; i<[tempWindow.subviews count]; i++) {
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard found, add the button
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
if([[keyboard description] hasPrefix:#"<UIPeripheralHost"] == YES)
[keyboard addSubview:doneButton];
} else {
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES)
[keyboard addSubview:doneButton];
}
}
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
myActiveTextField=textField;
if (myActiveTextField==txt_hour_section || myActiveTextField==txt_minute_section || myActiveTextField==txt_transport_time){
numberPadShowing=TRUE;
UIScrollView *scrollView =[[UIScrollView alloc] init];
scrollView=scroll_view;
svos = scrollView.contentOffset;
CGPoint pt;
CGRect rc = [textField bounds];
rc = [textField convertRect:rc toView:scrollView];
pt = rc.origin;
pt.x = 0;
pt.y -= 60;
[scrollView setContentOffset:pt animated:YES];
}
else{
numberPadShowing=FALSE;
}
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
UIScrollView *scrollView =[[UIScrollView alloc] init];
scrollView=scroll_view;
[scrollView setContentOffset:svos animated:YES];
[textField resignFirstResponder];
return YES;
}
Thanks in advances...
I think your problem is becuase you are tapping in to the numeric field after tapping the alpha field, and the alpha keyboard is already in view. Therefore when you tap the number field (whilst alpha keyboard still in view) it doesn't fire the 'keyboardDidShow' event, as the keyboard is already showing from the previous field (albeit a alpha one).
Try putting your done button logic into a delegate method for the textfield 'textFieldDidBeginEditing' event for the number field.
If my first answer doesn't work in your implementation, then you might want to consider a restructure and go for setting the inputAccessoryView of your UITextField. Code along these lines should do the trick (you can put this in your viewDidLoad nib, and also note that you'll need to implement the doneTapped method yourself);
// Create a UIToolbar object and set its style to be black and transparent
numericInputAccessoryView_ = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
numericInputAccessoryView_.barStyle = UIBarStyleBlackTranslucent;
// The flexible space item will push the done button to the far right
UIBarButtonItem *space = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
// Make the done button, this is a system item so you don't need to set the title or anything. This will call a method called "doneTapped:(id)sender" when you tap it, you'll need to implement that yourself.
UIBarButtonItem *done = [[UIBarButtonItem alloc]WithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneTapped:)];
// Stick the flexible space and the done button in your toolbar
numericInputAccessoryView_.items = [NSArray arrayWithObjects:space,done, nil];
// This would return it, if you had this in a separate method.
return numericInputAccessoryView_;
I am new to iPhone technology. I've saved a problem in a iPhone application, but when i use textfield keyboard type number pad the keyboard doesn't show a return/done button.
How can I hide the number pad keyboard after entering the number in the textfield? Do you have any solutions for that. Please provide help.
You can do this in this way:
#property (nonatomic, strong) UITextField *textField;
- (void)viewDidLoad {
[super viewDidLoad];
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonDidPressed:)];
UIBarButtonItem *flexableItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL];
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[self class] toobarHeight])];
[toolbar setItems:[NSArray arrayWithObjects:flexableItem,doneItem, nil]];
self.textField.inputAccessoryView = toolbar;
}
- (void)doneButtonDidPressed:(id)sender {
[self.textField resignFirstResponder];
}
+ (CGFloat)toolbarHeight {
// This method will handle the case that the height of toolbar may change in future iOS.
return 44.f;
}
you must be using UIKeyboardTypeNumberPad, try this instead UIKeyboardTypeNumbersAndPunctuation,
It'll not only show the return key but also provide you with some extra punctuations
There is no return or done key in number pad. You can do one thing when user touch outside of the textfield you can hide the keyboard. You should do something like this -
if (there is a touch outside of your textField)
{
[textField resignFirstResponder];
}
This question has already been answered, I know because thats how i got the solution. You have 2 options, first is to hide the keyboard by implementing a touch on the mainview that will send the "finished editing" message. that Hides the keyboard [self.view endEditing:YES];
If you do add the touch listener to the mainview you have to implement a condition so that any other buttons keep working.
What you do want to do to simulate a return key is to actually add it like this:
Register for a keyboard did show notification and add this to the code:
if ([self.passCodeLabel isFirstResponder])
{
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 163, 106, 53);
//doneButton.frame = CGRectMake(0, 163, 257, 257);
doneButton.adjustsImageWhenHighlighted = NO;
[doneButton setImage:[UIImage imageNamed:#"doneup.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:#"donedown.png"] forState:UIControlStateHighlighted];
[doneButton addTarget:self action:#selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
// locate keyboard view
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
NSLog(#"%#",[[UIApplication sharedApplication] windows]);
UIView* keyboard;
NSLog(#"Shared applicaiton windows count:%i",tempWindow.subviews.count);
for(int i=0; i<[tempWindow.subviews count]; i++) {
keyboard = [tempWindow.subviews objectAtIndex:i];
NSLog(#"%#",[keyboard description]);
// keyboard view found; add the custom button to it
if([[keyboard description] hasPrefix:#"<UIPeripheralHostView"] == YES)
{
NSLog(#"Adding return button");
[keyboard addSubview:doneButton];
}
}
}
This will add your own "done" button image to the keyboard (which you can just copy by taking a screenshot of the screen of the blank slot and adding the done text).
Btw the code i pasted works on my layout. For yours you might have to modify it a bit, but the principle is the same.
Create the button
Look for the keyboard subview
Add the button to that subview
- (void)viewDidLoad
{
[super viewDidLoad];
UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
numberToolbar.barStyle = UIBarStyleBlackTranslucent;
numberToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(doneWithNumberPad)],
nil];
[numberToolbar sizeToFit];
_txt_mobileno.inputAccessoryView = numberToolbar;
}
-(void)doneWithNumberPad
{
[_txt_mobileno resignFirstResponder];
}
I've set up a Button and add it to a view. I want to add a "done" button to the UIKeyboardTypeNumberPad. Here's my code.
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 163, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
[doneButton setImage:[UIImage imageNamed:#"DoneUp.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:#"DoneDown.png"] forState:UIControlStateHighlighted];
[doneButton addTarget:self action:#selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
// locate keyboard view
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for(int i=0; i<[tempWindow.subviews count]; i++) {
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard view found; add the custom button to it
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES)
[keyboard addSubview:doneButton];
}
Everything works great until I want to remove the button if I've got a Kayboard of type NumbersAndPunctuation for example.
If I click the button I use [(UIButton)*sender removeFromSuperview];
to prevent memory leaks.
But how do I remove the button from within an other function?
Thanks a lot!
Some other guys did ask that question somewhere else but didn't get a answer. I'am sure you can help :)
// Where self is a UIView subclass
NSLog(#"subviews: %#",self.subviews);
for(id view in self.subviews ){
if ([view isKindOfClass:[UIButton class]]) {
NSLog(#"Removing a button!");
[view removeFromSuperview];
}
}
You should store a reference to the button, instead of using a local variable. For example:
Header file:
#interface myObject : NSObject {
UIButton *doneButton;
...
Implementation file:
doneButton = [UIButton buttonWithType: UIButtonTypeCustom
...
To remove it (assuming you're in the same object:
[doneButton removeFromSuperview];
However, Apple may not take kindly to you adding buttons to their keyboard.
you can declare your button in your .h file, so you will be able to get access from all class methods