我已经阅读了很多关于此的问题,但找不到解决方案。
我只是不会在我的 NumberPad 上添加完成按钮
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardWillShowNotification
object:nil];
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)addButtonToKeyboard {
NSLog(@"call");
// create custom button
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 163, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
[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;
NSLog(@"%d", [tempWindow.subviews count]);
for(int i=0; i<[tempWindow.subviews count]; i++) {
NSLog(@"found");
keyboard = [tempWindow.subviews objectAtIndex:i];
[keyboard addSubview:doneButton];
}
}
- (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];
}
}
这段代码:
NSLog(@"%d", [tempWindow.subviews count]);
始终为 0,因此它根本不添加按钮……知道吗?