为UIKeyboardTypeNumberPad增加自定义按键

来源:互联网 发布:阿里云 腾讯云哪个好 编辑:程序博客网 时间:2024/05/15 08:09

在iphone上面(ipad上没这问题)把键盘设为NumberPad之后,会发现那个”return”键没有了,而这时候你又不想很土的在text field旁边自己加个按钮来做诸如完成输入/dismiss键盘之类的动作,其实这个是有解药的。

有没有发现最底下那行的左边有个空位,在那里补一个”return”键不就可以了么。这时候你需要这2个png(分别是doneup.png和donedown.png):

 

如果你是在iOS4上面写的话,你会需要这2个,因为iOS4上面的软键盘左下角没圆角:

 

接着就开始写代码来把贴有这个图片的UIButton在需要的时候加到那个空位那里了。幸好有专门的notification是帮你做这件事情的。所以在需要键盘出来之前,例如是点击UITextField的当下,往NSNotificationCenter注册一下这个notification就可以了。

?
01
02
03
04
05
06
07
08
09
10
11
12
13
// add observer for the respective notifications
// (depending on the os version)
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
    [[NSNotificationCenterdefaultCenter] addObserver:self
                      selector:@selector(addButtonToKeyboard)
                        name:UIKeyboardDidShowNotification
                                               object:nil];
}else{
    [[NSNotificationCenterdefaultCenter] addObserver:self
                      selector:@selector(addButtonToKeyboard)
                        name:UIKeyboardWillShowNotification
                                               object:nil];
}

在iphone 3.2 SDK之前,注册的是 UIKeyboardWillShowNotification,iOS4开始就要用 UIKeyboardDidShowNotification了。

然后在用完键盘之后要记得从NSNotificationCenter注销掉,不然你的别的软键盘都会粘上这个按钮了。

?
1
[[NSNotificationCenterdefaultCenter] removeObserver:self];

最后就是要在 addButtonToKeyboard 这个方法里面把keyboard view找出来,把这个”DONE”的UIButton给粘上去了就完事了。前人已经找到了其实keyboard view就是在app的第二个UIWindow(看这里)。遍历一下这个UIWindow的孩子们,找到keyboard view,然后把UIButton贴到左下角。需要注意的是iOS4的keyboard view的description跟iphone3.2之前的是不一样的噢。

?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
- (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(inti=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];
        }
    }
}

最后把你想象里面按了这个”DONE”键会发生的事情例如resignFirstResponder 之类的,写在 doneButton 方法里面就完成了。



Posted from Hangzhou, Zhejiang, China.

 分享到新浪微博
原创粉丝点击