iPhone 응용 프로그램에서 작업 중입니다. 보기 중 하나에 네 개의 텍스트 필드가 있습니다. 이 4 개의 텍스트 필드에는 숫자 패드 키보드를 사용할 텍스트 필드가 하나 있습니다. 그러나 숫자 패드 키보드에는 리턴 키가 없습니다. 그래서 다음 코드를 사용하여 프로그래밍 방식으로 "완료"버튼을 추가했습니다.숫자 패드 키보드 전용 버튼
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: %@", contactno.text);
[contactno resignFirstResponder];
}
이 텍스트 필드에는 문제가 없습니다. 그러나이 버튼은 나머지 텍스트 필드에도 추가됩니다. 어떻게 다른 텍스트 필드에 대해이 단추를 숨길 수 있습니다.
미리 감사드립니다.
http://cocoacontrols.com/platforms/ios/controls/amkeyboardnumberpad이 구현에서 살펴 해제 탭 이벤트를 캡처하고 원인 전체 UI를 통해 투명
UIView
추가 , 나를 위해 매우 잘 작동 –이 일을 정말 끔찍한 일입니다. 키보드의 구현이 변경되면 쉽게 깨질 수 있습니다. iPad에서는 작동하지 않으며 특히 iOS 5의 분할 키보드에서는 작동하지 않습니다. –