这是我的第一篇文章,我对iOS编程很新。我正在使用xcode 4.3,这可能是一个简单的问题。我已经看了一下将键盘放在键盘上方的方法,上面有一个上一个/下一个按钮。当xcode使用界面构建器时,我发现了几个关于如何执行此操作的内容。但我找不到最新版本的xcode。所以我已经在互联网和Apple开发网站上找到了我所发现的东西,并提出了一些代码。唯一的是,它不起作用!我收到一个错误,表示使用inputAcessoryView分配给行上的readonly属性。我知道它可能很简单,但是当将代码从先前版本转移到新版本时,我很可能搞砸了。有人可以检查我的代码,让我知道我做错了什么。非常感谢。我已经包含了我的.h / .m文件
#import <UIKit/UIKit.h>
@interface keyboardViewController : UIViewController
@property (retain, nonatomic) IBOutlet UIView *textField1;
@property (retain, nonatomic) IBOutlet UIView *textField2;
@property (nonatomic, retain) UIToolbar *keyboardToolbar;
-(void)resignKeyboard:(id)sender;
-(void)previousField:(id)sender;
-(void)nextField:(id)sender;
@end
执行力度
#import "keyboardViewController.h"
@interface keyboardViewController ()
@end
@implementation keyboardViewController
@synthesize textField1;
@synthesize textField2;
@synthesize keyboardToolbar;
- (void)viewDidLoad
{
[super viewDidLoad];
if (keyboardToolbar == nil)
{
keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
UIBarButtonItem *previousButton = [[UIBarButtonItem alloc] initWithTitle:@"previous"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(previousField:)];
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithTitle:@"next"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(nextField:)];
UIBarButtonItem *extraSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemFlexibleSpace) target:nil action:nil];
UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemDone) target:self action:@selector(resignKeyboard:)];
[keyboardToolbar setItems:[[NSArray alloc] initWithObjects:previousButton, nextButton, extraSpace, done, nil]];
textField1.inputAccessoryView = keyboardToolbar;
textField2.inputAccessoryView = keyboardToolbar;
}
}
-(void) resignKeyboard:(id)sender
{
if ([textField1 isFirstResponder])
[textField1 resignFirstResponder];
else if ([textField2 isFirstResponder])
[textField2 resignFirstResponder];
}
-(void) previousField:(id)sender
{
if ([textField1 isFirstResponder])
[textField2 becomeFirstResponder];
else if ([textField2 isFirstResponder])
[textField1 becomeFirstResponder];
}
-(void) nextField:(id)sender
{
if ([textField1 isFirstResponder])
[textField2 becomeFirstResponder];
else if ([textField2 isFirstResponder])
[textField1 becomeFirstResponder];
}
- (void)viewDidUnload
{
[self setTextField1:nil];
[self setTextField2:nil];
[self setKeyboardToolbar:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end
答案 0 :(得分:3)
@interface keyboardViewController : UIViewController<UITextFieldDelegate>
@property (retain, nonatomic) IBOutlet UITextField *textField1;
@property (retain, nonatomic) IBOutlet UITextField *textField2;
并在.m位置而不是此代码
textField1.inputAccessoryView = keyboardToolbar;
textField2.inputAccessoryView = keyboardToolbar;
粘贴这些代码
for (id view in self.view.subviews) {
if ([view isKindOfClass:[UITextField class]]) {
[(UITextField *)view setInputAccessoryView:keyboardToolbar];
}
}
粘贴此代码
-(void) resignKeyboard:(id)sender{
[self.view endEditing:YES];
}
而不是
-(void) resignKeyboard:(id)sender
{
if ([textField1 isFirstResponder])
[textField1 resignFirstResponder];
else if ([textField2 isFirstResponder])
[textField2 resignFirstResponder];
}
在viewDidLoad方法中添加此代码
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
和
-(void)nextField:(id)sender{
if ([textField1 isFirstResponder]) {
[textField2 becomeFirstResponder];
[previousButton setEnabled:YES];
[nextButton setEnabled:NO];
}
}
-(void)previousField:(id)sender{
if ([textField2 isFirstResponder]) {
[textField1 becomeFirstResponder];
[nextButton setEnabled:YES];
[previousButton setEnabled:NO];
}
}
-(void)keyboardWillShow:(id)sender{
if ([textField2 isFirstResponder]) {
[nextButton setEnabled:NO];
[previousButton setEnabled:YES];
}else if ([textField1 isFirstResponder]) {
[nextButton setEnabled:YES];
[previousButton setEnabled:NO];
}
}
答案 1 :(得分:2)
当您的文本字段类型为UIKeyboardTypeNumberPad时,即将添加“完成按钮”时,UIToolbar非常有用。这是因为它默认没有完成按钮。为此,您只需添加以下内容: -
UIToolbar *boolbar = [UIToolbar new];
boolbar.barStyle = UIBarStyleBlackTranslucent;
[boolbar sizeToFit];
UIBarButtonItem *leftBarButton =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneBtnClicked)];
NSArray *array = [NSArray arrayWithObjects:cancelleftBarButton, nil];
[leftBarButton release];
[boolbar setItems:array];
textField_phnNumber.inputAccessoryView = boolbar;
//由selector
执行的方法- (无效)doneBtnClicked {
[textField_phnNumber resignFirstResponder];
}
注意: - 如果您使用的是UITextField,请在textFieldDidBeginEditing委托方法中添加此内容。
答案 2 :(得分:0)
此代码在键盘上方的UIToolbar的右侧侧创建“完成”按钮:
UIToolbar *tbAboveKeyboard = [[UIToolbar alloc] init];
tbAboveKeyboard.barStyle = UIBarStyleBlackTranslucent;
[tbAboveKeyboard sizeToFit];
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *rightBarButton =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonClicked:)];
NSArray *array = [NSArray arrayWithObjects:flexibleSpaceLeft, rightBarButton, nil];
[rightBarButton release];
[tbAboveKeyboard setItems:array];
yourTextView.inputAccessoryView = tbAboveKeyboard;
[tbAboveKeyboard release];
...
- (void)doneButtonClicked:(id)sender
{
[searchTextField resignFirstResponder];
}