「完了」ボタンが表示されるように UIKeyboardTypeNumberPad を実装するにはどうすればよいですか? デフォルトでは、1 つもありません。
4 に答える
私が間違っていなければ、カスタムの「完了」ボタンを UIKeyboardTypeNumberPad のキーボードに追加する方法について質問したいと思います。その場合、これは役立つかもしれません。.h で UIButton *doneButton を宣言し、次のコードを .m ファイルに追加します。
- (void)addButtonToKeyboard {
// create custom button
if (doneButton == nil) {
doneButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 163, 106, 53)];
}
else {
[doneButton setHidden:NO];
}
[doneButton addTarget:self action:@selector(doneButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
// locate keyboard view
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard = nil;
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)doneButtonClicked:(id)Sender {
//Write your code whatever you want to do on done button tap
//Removing keyboard or something else
}
私は自分のアプリケーションで同じものを使用しており、ボタンのフレームが調整されているため、キーボード上に完了ボタンを表示する必要があるときはいつでも [self addButtonToKeyboard ] を呼び出すことができます。それ以外の場合、UIKeyboardTypeNumberPad には [完了] ボタンがありません。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];
//Call this method
- (void)keyboardWillShow:(NSNotification *)note {
UIButton *doneButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 163, 106, 53)];
doneButton.adjustsImageWhenHighlighted = NO;
[doneButton setImage:[UIImage imageNamed:@"Done.png"] forState:UIControlStateNormal];
[doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for(int i=0; i<[tempWindow.subviews count]; i++) {
keyboard = [tempWindow.subviews objectAtIndex:i];
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];
}
}
}
iOS 5 の問題が解決しました。どうもありがとう。
[完了] ボタンの表示に問題がありました。
置換:
if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
[keyboard addSubview:doneButton];
iOS 5 で完了ボタンが表示されませんでした...
と:
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];
}
御馳走を働いた。あなたはスターです。ありがとう、ニコラ ;-)
同じアプリで他のタイプのキーボードを読み込もうとすると [DONE] ボタンがポップアップし続けるという問題があった場合、多くのアプリでこの問題が発生していることを知っています。とにかく、これが私がこれを解決した方法です:あなたのViewController(DONEボタンを追加したのと同じviewcontroller)にこのコードを(そのまま)追加すると、DONEボタンが再表示され続ける問題を解決するはずです。一部の人々に役立つことを願っています。
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
} else {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
}