2011-04-28 5 views
0

하나의 텍스트 필드가 phonenumber textfield이고 다섯 개의 텍스트 필드를 가지고있는 하나의 등록 양식을 가지고 있는데, 우리는 그 숫자 키보드에 대해 키보드를 사용하지만 numberpadkeyboard에는 완료 버튼이 없습니다. 나는 버튼을 추가했지만 버튼은 모든 텍스트 필드에 추가되지만 phonenumber 필드 만 원한다. 이 문제를 해결하는 방법을 알려주십시오. 내 코드입니다 .. 사전에숫자 패드 키보드에 완료 버튼 추가하기

- (void)viewDidLoad 
{ 
    self.navigationController.navigationBarHidden = NO; 
    self.navigationController.navigationBar.tintColor = [UIColor blackColor]; 

    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(backButtonAction)]; 
    self.navigationItem.leftBarButtonItem = leftButton; 
// UIBarButtonItem *rightbarButton = [[UIBarButtonItem alloc]initWithTitle:@"About" style:UIBarButtonItemStyleBordered target:self action:@selector(AboutButtonAction)]; 
// self.navigationItem.rightBarButtonItem = rightbarButton; 
    [super viewDidLoad]; 
    NSLog(@"Events Id is : %@", particularEventId); 
    phoneNumber = [[UITextField alloc] initWithFrame:CGRectMake(56, 223, 220, 31)]; 
    phoneNumber.borderStyle = UITextBorderStyleRoundedRect; 
    phoneNumber.keyboardType = UIKeyboardTypeNumberPad; 
    phoneNumber.returnKeyType = UIReturnKeyDone; 
    phoneNumber.textAlignment = UITextAlignmentLeft; 

    [self.view addSubview:phoneNumber]; 
    // 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(keyboard == phoneNumber) 
     { 
     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: %@", phoneNumber.text); 
    [phoneNumber resignFirstResponder]; 
} 

감사 :

내 프로젝트에이 코드를 사용
+0

이것 좀 되세요 [답변. (http://stackoverflow.com/a/20192857/1603072) – Bhavin

답변

2

, 나는 그것이 최적화되지 생각하지만 그것은 매우 잘 작동합니다.

.H

BOOL firstTime; 
BOOL add; 
BOOL keyboardOpened; 

하는 .m

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    firstTime = TRUE; 
    add = TRUE; 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil]; 
} 

- (void)addButtonToKeyboard { 
    // create custom button 
    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    doneButton.frame = CGRectMake(0, 163, 106, 53); 
    doneButton.adjustsImageWhenHighlighted = NO; 
    [doneButton setImage:[UIImage imageNamed:@"DoneUp3.png"] forState:UIControlStateNormal]; 
    [doneButton setImage:[UIImage imageNamed:@"DoneDown3.png"] forState:UIControlStateHighlighted]; 
    doneButton.tag = 3; 
    [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 ([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES && add) [keyboard addSubview:doneButton]; 
    } 
} 

- (void)removeButtonFromKeyboard 
{ 
    // 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, remove the button 
     if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES) [[keyboard viewWithTag:3] removeFromSuperview]; 
    } 
} 

- (void)doneButton:(id)sender { 
    [firstResponder resignFirstResponder]; 
    if (![[[UIDevice currentDevice] model] isEqualToString:@"iPad"] && ![[[UIDevice currentDevice] model] isEqualToString:@"iPad Simulator"]) 
    { 
     [self removeButtonFromKeyboard]; 
     firstTime = TRUE; 
    } 
} 

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField { 
    [theTextField resignFirstResponder]; 
    return YES; 
} 

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    firstResponder = textField; 

    for (int i = 0; i < [[[profile operationObject] keys] count]; i++) 
    { 
     if ([[[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]] viewWithTag:1] isEqual:textField]) 
     { 
      editingPath = [NSIndexPath indexPathForRow:i inSection:0]; 
      break; 
     } 
    } 

    if (![[[UIDevice currentDevice] model] isEqualToString:@"iPad"] && ![[[UIDevice currentDevice] model] isEqualToString:@"iPad Simulator"]) 
    { 
     if(!firstTime) 
     { 
      if(textField.keyboardType == UIKeyboardTypeNumberPad) 
      { 
       add = TRUE; 
       [self addButtonToKeyboard]; 
      } 
      else 
      { 
       add = FALSE; 
      } 
     } 
     else 
     { 
      firstTime = FALSE; 
      if(textField.keyboardType != UIKeyboardTypeNumberPad) 
      { 
       add = FALSE; 
      } 
     } 
     keyboardOpened = TRUE; 
    } 
} 

- (void)keyboardDidShow:(id)sender 
{ 
    if (![[[UIDevice currentDevice] model] isEqualToString:@"iPad"] && ![[[UIDevice currentDevice] model] isEqualToString:@"iPad Simulator"]) 
    { 
     NSLog(@"%@",[[UIDevice currentDevice] model]); 
     [self addButtonToKeyboard]; 
     keyboardOpened = TRUE; 
    } 
} 

- (void)textFieldDidEndEditing:(UITextField *)textField 
{ 
    [[[profile operationObject] values] replaceObjectAtIndex:[editingPath row] withObject:[textField text]]; 
    firstResponder = nil; 

    if (![[[UIDevice currentDevice] model] isEqualToString:@"iPad"] && ![[[UIDevice currentDevice] model] isEqualToString:@"iPad Simulator"]) 
    { 
     [self removeButtonFromKeyboard]; 
     keyboardOpened = FALSE; 
    } 
} 
관련 문제