3

这是我的第一篇文章,我对 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
4

3 回答 3

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];
    }
}
于 2012-04-26T12:14:54.063 回答
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;

//选择器执行的方法

-(无效)doneBtnClicked{

[textField_phnNumber resignFirstResponder];

}

注意:-如果您使用 UITextField,请将其添加到 textFieldDidBeginEditing 委托方法中。

于 2012-04-26T12:29:32.547 回答
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];
}
于 2013-01-28T11:53:01.553 回答