这是我的第一篇文章,我对 iOS 编程很陌生。我正在使用 xcode 4.3 并且有什么可能是一个简单的问题。我已经到处寻找一种将工具栏放置在键盘上方的方法,该键盘上有一个上一个/下一个按钮。当 xcode 使用界面生成器时,我发现了一些关于如何执行此操作的 tuts。但我找不到适用于最新版本 xcode 的版本。所以我把我在互联网上和苹果开发网站上找到的东西拿出来了一些代码。唯一的问题是,它不起作用!我收到一个错误,指出在 inputAcessoryView 的行上分配给只读属性。我知道这可能很简单,但是当将代码从以前的版本转移到较新的版本时,我很可能搞砸了。有人可以检查我的代码并让我知道我做错了什么。非常感谢。我已经包含了我的 .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