键盘Iphone

时间:2009-05-20 19:28:29

标签: iphone keyboard touch iphone-softkeyboard

可以知道用户何时触摸键盘iphone?当用户触摸键盘上的某个按钮时......:/

1 个答案:

答案 0 :(得分:1)

最简单的方法是使用TextField。即使是你的用户界面也不需要一个,你可以将它的框架设置为零,这样它就不会显示在屏幕上。然后,您可以使用文本字段的委托回调方法访问按下的键。

- (void)viewDidLoad {
    [super viewDidLoad];
    //CGRectZero because we don't want the textfield to be shown onscreen
    UITextField *f = [[UITextField alloc] initWithFrame:CGRectZero];
    //We set the delegate so we can grab keypressed
    f.delegate = self; 
    [self.view addSubview:f];
    [f becomeFirstResponder];  //Show the keyboard
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
                                                       replacementString:(NSString *)string {
    if (string.length >0) {
       NSLog(@"%@ Pressed",string);
    }
    else {
       NSLog(@"Backspcae pressed");
    }        
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    NSLog(@"return pressed");
    return YES;
}

注意:为了避免编译器警告,请确保在.h文件中该类明确表示它实现了UITextFieldDelegate协议。即:

@interface MyViewController : UIViewController <UITextFieldDelegate>