Nit*_*tin 2 iphone objective-c ios
我正在研究iPhone应用程序.我在其中一个视图中有四个文本字段.在这四个文本字段中,我有一个文本字段,其中将使用数字键盘.但在数字键盘上没有返回键.所以我通过使用以下代码以编程方式添加了"完成"按钮.
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];
}
Run Code Online (Sandbox Code Playgroud)
这个特定的文本域很好.但是这个按钮也可以添加到其他文本字段中.如何取消隐藏其他文本字段的按钮.

非常感谢.
Dav*_*ong 23
请,请,请,请,请,请请,请请请请不要这样做. 这太荒谬了.考虑一下:
这些只是将您的UI重写为私有视图层次结构所带来的无数问题.
以下是两种选择:
-inputAccessoryView在每个UIResponder(因此每个UIView)上使用该属性来定义UIToolbar带有"完成"按钮的属性.当inputAccessoryView键盘进出动画时,它将位于键盘上方.
UIView在整个UI上添加透明,捕获点击事件并使键盘解除
| 归档时间: |
|
| 查看次数: |
5236 次 |
| 最近记录: |