[英]trying to add done button to Numeric keyboard
我正在嘗試向UIKeyboadnumpad添加一個“完成”按鈕,但沒有成功。 我的代碼有什么問題?
鍵盤沒有完成按鈕
@implementation DemoViewController
- (void)loadView {
self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 26)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.keyboardType = UIKeyboardTypeNumberPad;
textField.returnKeyType = UIReturnKeyDone;
textField.textAlignment = UITextAlignmentLeft;
textField.text = @"12345";
[self.view addSubview:textField];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
}
- (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:0];
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 ([[[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)doneButton:(id)sender {
NSLog(@"Input: %@", textField.text);
[textField resignFirstResponder];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[textField release];
[super dealloc];
}
@end
另一種方法。 如果屏幕上有其他非數字鍵盤文本字段,則非常完美。
- (void)viewDidLoad
{
[super viewDidLoad];
UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
numberToolbar.barStyle = UIBarStyleBlackTranslucent;
numberToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:@"Apply" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)],
nil];
[numberToolbar sizeToFit];
numberTextField.inputAccessoryView = numberToolbar;
}
-(void)cancelNumberPad{
[numberTextField resignFirstResponder];
numberTextField.text = @"";
}
-(void)doneWithNumberPad{
NSString *numberFromTheKeyboard = numberTextField.text;
[numberTextField resignFirstResponder];
}
我需要電話墊(帶+ *#)而不是數字鍵盤,我甚至沒有角落里的空按鈕。
有些教程不完整或者更老。 本教程從IOS 4.3開始工作,我檢查了它。 保存兩個圖像圖形,並粘貼代碼。 幾乎無法改變。 鏈接在這里。
PS。 我不是以任何方式與本文有任何關聯,但發現它是完整的。
http://www.neoos.ch/blog/37-uikeyboardtypenumberpad-and-the-missing-return-key
在- (void)keyboardDidShow:(NSNotification *)note
寫下你的添加按鈕的代碼
代替
- (void)keyboardWillShow:(NSNotification *)note
對於此工具, UIKeyboardDidShowNotification
通知如下:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification object:nil];
我認為UIView* keyboard;
沒有得到鍵盤的視圖,因為鍵盤尚未顯示,它將顯示!
簡而言之:截取屏幕截圖,剪切整個退格鍵,水平翻轉,在Photoshop中清除其退格符號,並在返回鍵上覆蓋我們想要的文本。 我們選擇將其標記為DONE 。
現在我們有自定義按鈕的UIControlStateNormal的圖像。
重復整個過程(在截取屏幕截圖時使用觸摸的退格鍵)以獲取按鈕的UIControlStateHighlighted
的第二個圖像。
結果如下:< 缺少圖像 >
現在回到編碼:
首先,我們需要知道數字鍵盤何時在屏幕上向上滑動,以便我們可以在此之前插入自定義按鈕。
幸運的是,有一個關於這個目的的通知,並注冊它就像:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
一旦完成整個事情,不要忘記在適當的地方從通知中心刪除觀察者 :
[[NSNotificationCenter defaultCenter] removeObserver:self];
現在我們已經了解它的核心。 我們在keyboardWillShow
方法中所要做的就是找到鍵盤視圖並添加我們的按鈕。
鍵盤視圖是我們應用程序的第二個UIWindow的一部分,正如其他人已經想到的那樣(參見此主題)。
所以我們引用那個窗口(在大多數情況下它將是第二個窗口,所以下面的代碼中的objectAtIndex:1
很好),遍歷它的視圖層次結構,直到找到鍵盤並在左下方添加按鈕:
- (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];
}
}
Voilà,就是這樣!
我們按鈕的空白區域從坐標(0,163)開始,並具有尺寸(106,53)。
當然,必須現在編寫doneButton
方法,但這不再困難。 只需確保在正在編輯的文本字段上調用resignFirstResponder
以使鍵盤向下滑動。
我們“完成了”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.