您的位置:首页 > 移动开发 > Objective-C

iPhone 自定义键盘按键

2011-11-09 23:43 288 查看
首先注册消息通知UIKeyboardWillShowNotification :

    [[NSNotificationCenter defaultCenter] addObserver:self 

                                             selector:@selector(keyboardWillShow:) 

                                                 name:UIKeyboardWillShowNotification 

                                               object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self                                                          

selector:@selector(keyboardWillHide:)                                                                  

name:UIKeyboardWillHideNotification

object:nil];

- (IBAction)keyboardWillHide:(NSNotification *)note

{

    // 做自己要做的事情

}
在消息回叫函数中处理--添加按键

- (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];    //控件 也是view 又可以分成多个控件单元 入tableView cell

            break;

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息