ios4.0以前,尝试过修改UIKeyboardTypeNumberPad,为其添加done按键,那时只需要找到keyboardview,done作为subview添加上。后来发现这个方法在新的sdk中无效了,后来查到下面这种方式。
There are several issues which you must be aware of when working around the missing done button on the iPhone numeric keypad. Use of private API’s is frowned upon by apple and they can reject your application. Also a lot of the solutions out there don’t work very well for OS4. This solution works by drawing a button on top of the keypad and animating it off the screen when required. It also draws the text so you don’t have to rely on images.
Firstly add a header file call it something like NumberKeypadModController.h also create an M file. Add the follow code to your H file.
#import <Foundation/Foundation.h>
@protocol NumberKeypadModControllerDelegate <NSObject> - (void) donePressed:(id)sender; @end /** @property (nonatomic, retain) NSTimer *fixKeyboardTimer; @property (nonatomic, retain) UIButton *doneButton; @property (nonatomic, retain) id<NumberKeypadModControllerDelegate> delegate; - (void)textFieldShouldBeginEditing:(UITextField *)textField; - (void)resignedResponderWithView:(UIView*)textField; @end |
And your M file
#import “NumberKeypadModController.h”
@interface NumberKeypadModController (PrivateMethods) #define PRE_SLIDING_DELAY_DURATION 0.1 #define USE_TEXTURED_BUTTON 0 @implementation NumberKeypadModController @synthesize fixKeyboardTimer; @synthesize doneButton; @synthesize delegate; - (id)init { [doneButton addTarget:self action:@selector(donePressed) forControlEvents:UIControlEventTouchUpInside]; self.currentTextField = nil; showHideCounter = 0; - (void)textFieldShouldBeginEditing:(UITextField *)textField { - (void)textFieldDidBeginEditing:(UITextField *)textField { // NSLog(@”textFieldDidBeginEditing”); if (!doneButtonShownRecently) - (void)textFieldShouldEndEditing:(UITextField *)textField { - (void) textFieldDidEndEditing:(UITextField *)textField { - (void)resignedResponderWithView:(UIView*)textField { - (void) addDoneToKeyboard { doneButton.hidden = NO; //Add a button to the top, above all windows // check if top window is of keypad or else if(topWindowIndex < 0) topWindow = [allWindows objectAtIndex:topWindowIndex]; if(topWindowIndex < 0) { if (doneButton.superview) [topWindow addSubview:doneButton]; if (!doneButtonShownRecently) { doneButtonShown = YES; - (void) removeDoneFromKeyboard { [fixKeyboardTimer invalidate]; if (!doneButtonShownRecently) { doneButtonShown = NO; - (void)considerDoneButtonReallyHidden { - (void) donePressed { - (void) dealloc { @end |
Now to your view header file add an import to NumberKeypadModController.h
Now its imple matter of adding a delegate to your interface and a property on the view your going to use the code. In your viewdidload add this code.
@interface YourViewController : UIViewController <UITextFieldDelegate, NumberKeypadModControllerDelegate> {
NumberKeypadModController *numberKeyPadModController; @property(nonatomic,retain) NumberKeypadModController *numberKeyPadModController; @end |
And your m file.
#import “YourViewController.h” #import “NumberKeypadModController.h”
@interface UIView (FindAndResignFirstResponder) @implementation YourViewController @synthesize numberKeyPadModController; - (void)viewDidLoad { self.numberKeyPadModController = [[[NumberKeypadModController alloc] init] autorelease]; [[NSNotificationCenter defaultCenter] addObserver:self textFieldRow1.delegate = self; - (void)doneButtonPressed:(UITextField*)sender{ - (void) donePressed:(id)sender { - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { return YES; - (void) textFieldDidBeginEditing:(UITextField *)textField { - (BOOL) textFieldShouldEndEditing:(UITextField *)textField { -(void)touchesBegan(NSSet *)touches withEvent:(UIEvent *)event { [super touchesBegan:touches withEvent:event]; - (void)didReceiveMemoryWarning { - (void)viewDidUnload { - (void)dealloc { @end |
Now in interface builder add a UITextField and assign the outlet, save and run.