gpt4 book ai didi

iphone - 尝试将完成按钮添加到数字键盘

转载 作者:可可西里 更新时间:2023-11-01 03:02:48 37 4
gpt4 key购买 nike

我正在尝试向 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

最佳答案

另一种解决方案。如果屏幕上还有其他非数字键盘文本字段,那就完美了。

inputAccessoryView

- (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];
}

我需要电话键盘(带有 +*#)而不是数字键盘,我什至没有角落里的空白按钮。

关于iphone - 尝试将完成按钮添加到数字键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9079815/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com