Copyright (C) iPhoneアプリ開発備忘録 All rights reserved.
ブログ内で記したコード、内容の正確性は保証いたしません。
記載内容を実装したことにより発生した不具合・損害等の責任は一切負いません。



2009年11月28日

数字テンキーUIKeyboardTypeNumberPadにReturnボタンを付ける



数字テンキーのUIKeyboardTypeNumberPadには、なぜかキーボードを閉じるReturnボタンがない。
カスタマイズによりReturnボタンを設置する方法が下記サイトに掲載されている。

http://www.neoos.ch/news/46-development/54-uikeyboardtypenumberpad-and-the-missing-return-key

■ポイント
・通知センタを使う。
・Returnボタン「DONE」のimageファイルをダウンロードして使う。
 
通知センタ登録
- (void)viewDidLoad {
NSNotificationCenter* center;
[center addObserver:self selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:nil];
}


通知センタ削除
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:nil];


数字テンキーボードに「DONE」ボタンを追加
- (void)keyboardWillShow:(NSNotification *)note {
// 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;
for(int i=0; i<[tempWindow.subviews count]; i++) {
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard view found; add the custom button to it
if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
[keyboard addSubview:doneButton];
}
}



UITextFieldに割り当てられた数字テンキーボードを閉じる
- (IBAction)doneButton:(id)sender {
[hogeTextField resignFirstResponder];

}

 
 
タグ:iPhone
【UIKeyboardTypeの最新記事】
posted by mobileDeveloper at 20:35 | Comment(0) | TrackBack(0) | UIKeyboardType はてなブックマーク - 数字テンキーUIKeyboardTypeNumberPadにReturnボタンを付ける | このブログの読者になる | 更新情報をチェックする
この記事へのコメント
コメントを書く
お名前:

メールアドレス:

ホームページアドレス:

コメント:

認証コード: [必須入力]


※画像の中の文字を半角で入力してください。
※ブログオーナーが承認したコメントのみ表示されます。

この記事へのトラックバック
Apple、Appleのロゴ、App Store、iPodのロゴ、iTunesは、米国および他国のApple Inc.の登録商標です。
iPhone、iPod touch、iPadはApple Inc.の商標です。
iPhone商標は、アイホン株式会社のライセンスに基づき使用されています。
その他、本ブログに記載されている製品名、会社名は、それぞれ各社の商標または登録商標です。