iOS Numeric keypad custom keys

Source: Internet
Author: User

Uikeyboardtypenumberpad Numeric Keypad custom keys Recently did a search user's function, here uses the Uisearchbar. Because the search is only mobile phone number, so the keyboard is limited to digital input, you can do this:
self.searchBar.keyboardType = UIKeyboardTypeNumberPad;


如果使用的不是搜索框而是textField输入框,可以设置textField的键盘属性来展示

self.textField.keyboardType = UIKeyboardTypeNumberPad;

监听事件如下所示即可。
 

But here's the problem, there is no "search" button on the numeric keypad, this way the user can not search after entering the mobile phone number. So at this point we need to add a custom search button to the keyboard.

Solution Ideas
    1. Custom Search button
    2. Listen for events that appear on the keyboard
    3. Traverse the Windows Form of the search, find the keyboard's form, and then traverse its child view to find the keyboard view we really need
    4. Add our custom buttons to the view found above

One thing to note here is that as the iOS SDK continues to evolve, the keyboard view name is constantly changing, and when you debug the following code that doesn't get the desired effect, iterate over the window sill and then slowly debug to find the name of the view you really need.

Resolving code 1. Customizing the Search button
// 搜索按钮    _searchButton = [UIButton buttonWithType:UIButtonTypeCustom];    _searchButton.frame = CGRectMake(0, 163, 106, 53);    [_searchButton setTitle:@"搜索" forState:UIControlStateNormal]; [_searchButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [_searchButton addTarget:self action:@selector(SearchButtonDidTouch:) forControlEvents:UIControlEventTouchUpInside]; 

2. Listen for events that appear on the keyboard
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShowOnDelay:) name:UIKeyboardWillShowNotification object:nil];- (void)keyboardWillShowOnDelay:(NSNotification *)notification { [self performSelector:@selector(keyboardWillShow:) withObject:nil afterDelay:0];}

In this case, the execution function after the listener is not immediately executing the lookup form function, because after iOS4, the keyboard added to the form of the event is placed in the next eventloop, so we adopted a deferred method.

3. Traverse the view and add a button
- (void) Keyboardwillshow: (Nsnotification *) Notification {UIView *foundkeyboard =NilUIWindow *keyboardwindow =Nilfor (UIWindow *testwindow in [[UIApplication sharedapplication] windows]) {if (![ [Testwindow class] isequal:[UIWindow class]]) {Keyboardwindow = Testwindow;Break } }if (!keyboardwindow)Returnfor (__StrongUIView *possiblekeyboard in [Keyboardwindow Subviews]) {if ([[[Possiblekeyboard description] hasprefix:@"<uiinputsetcontainerview"]) {for (__Strong  UIView *possiblekeyboard_2 in Possiblekeyboard. subviews) { if ([possiblekeyboard_2. Description Hasprefix: @"<uiinputsethostview"]) {foundkeyboard = Possiblekeyboard_2;}} }} if (foundkeyboard) { if ([[Foundkeyboard subviews] indexofobject:_searchbutton] = = nsnotfound) {[FOUNDK Eyboard Addsubview:_searchbutton]; } else {[Foundkeyboard Bringsubviewtofront:_searchbutton];         }}} 

iOS numeric keypad custom keys

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.