选择UITextField时,在iphone键盘中添加自定义图像按钮

时间:2010-10-30 06:41:02

标签: iphone uitextfield

当有人选择我的UITextField时,我想在iPhone键盘中添加一些自定义图像按钮。在iPhone键盘上有默认按钮返回(或关闭键盘)?我想将返回按钮更改为我自己的按钮并执行相同的功能,以关闭键盘。

有可能吗?任何人都可以帮我一些源代码吗?

1 个答案:

答案 0 :(得分:2)

是的,你可以这样做。这是源代码

- (void)loadView {
     self.view = [[UIView alloc] initWithFrame:[UIScreen
 mainScreen].applicationFrame];
     self.view.backgroundColor = [UIColor
 groupTableViewBackgroundColor];

     textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300,
 26)];
     textField.borderStyle = UITextBorderStyleRoundedRect;
     textField.keyboardType = UIKeyboardTypeNumberPad;
     textField.returnKeyType = UIReturnKeyDone;
     textField.textAlignment = UITextAlignmentLeft;
     textField.text = @"12345";
     [self.view addSubview:textField];
        // add observer for the respective notifications (depending on
 the os version)    if ([[[UIDevice
 currentDevice] systemVersion]
 floatValue] >= 3.2) {
        [[NSNotificationCenter
 defaultCenter] addObserver:self 
                                                 selector:@selector(keyboardDidShow:) 
                                                     name:UIKeyboardDidShowNotification 
                                                   object:nil];         } else {        [[NSNotificationCenter
 defaultCenter] addObserver:self 
                                                 selector:@selector(keyboardWillShow:) 
                                                     name:UIKeyboardWillShowNotification 
                                                   object:nil];     }    }

 - (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(int i=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];       }   } }

 - (void)keyboardWillShow:(NSNotification
 *)note {   // if clause is just an additional precaution, you could also
 dismiss it     if ([[[UIDevice
 currentDevice] systemVersion]
 floatValue] < 3.2) {       [self
 addButtonToKeyboard];  } }

 - (void)keyboardDidShow:(NSNotification
 *)note {   // if clause is just an additional precaution, you could also
 dismiss it     if ([[[UIDevice
 currentDevice] systemVersion]
 floatValue] >= 3.2) {      [self
 addButtonToKeyboard];
     } }


 - (void)doneButton:(id)sender {    NSLog(@"doneButton");
     NSLog(@"Input: %@", textField.text);
     [textField resignFirstResponder]; }

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
 {
     return (interfaceOrientation == UIInterfaceOrientationPortrait); }

 - (void)didReceiveMemoryWarning {
     [super didReceiveMemoryWarning]; }

 - (void)dealloc {
     [[NSNotificationCenter defaultCenter] removeObserver:self];
     [textField release];
     [super dealloc]; }

希望这会对你有所帮助。

更多信息:http://www.appsfor2012.com/2012/09/keyboard-with-custom-button.html