0


在我的 iPhone 应用程序中,我有一个带有多个 UITextField 的 UIScrollView。
使用 BSKeyboardControls 我添加了 Prev/Next/Done 按钮以在字段之间移动。但是,所选字段上的焦点不起作用,这意味着文本字段实际上仍在键盘下方,尽管已选中。
becomeFirstResponder已激活,但只是不设置焦点。
有什么想法可能是错的吗?
谢谢

在 H 文件中

#import "BSKeyboardControls.h"
...
@interface AddClientViewController : BaseViewController<UIAlertViewDelegate, UIScrollViewDelegate, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate, UITextViewDelegate, BSKeyboardControlsDelegate>
...
@property (strong, nonatomic) IBOutlet UITextField *firstName;
@property (strong, nonatomic) IBOutlet UITextField *lastName;
@property (strong, nonatomic) IBOutlet UITextField *email;
@property (strong, nonatomic) IBOutlet UITextField *mobile;
@property (strong, nonatomic) IBOutlet UITextField *birthday;
@property (strong, nonatomic) IBOutlet UITextField *anniversary;
@property (strong, nonatomic) IBOutlet UITextField *street;
@property (strong, nonatomic) IBOutlet UITextField *city;
@property (strong, nonatomic) IBOutlet UITextField *state;
@property (strong, nonatomic) IBOutlet UITextField *zip;
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;

@property (nonatomic, strong) BSKeyboardControls *keyboardControls;
....

在 M 文件中

- (void)viewDidLoad
{
...
    NSArray *fields = @[ self.firstName, self.lastName,
    self.email, self.mobile,
    self.birthday, self.anniversary,
    self.street, self.city, self.state, self.zip];

    [self setKeyboardControls:[[BSKeyboardControls alloc] initWithFields:fields]];
    [self.keyboardControls setDelegate:self];
}

- (void)keyboardControlsDonePressed:(BSKeyboardControls *)keyboardControls
{
    [keyboardControls.activeField resignFirstResponder];
}
- (void)keyboardControls:(BSKeyboardControls *)keyboardControls directionPressed:(BSKeyboardControlsDirection)direction
{
    UIView *view = keyboardControls.activeField.superview.superview;
    [self.scrollView scrollRectToVisible:view.frame animated:YES];
}
- (void)keyboardControls:(BSKeyboardControls *)keyboardControls selectedField:(UIView *)field inDirection:(BSKeyboardControlsDirection)direction
{
    UIView *view = keyboardControls.activeField.superview.superview;
    [self.scrollView scrollRectToVisible:view.frame animated:YES];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self.keyboardControls setActiveField:textField];
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    [self.keyboardControls setActiveField:textView];
}

BSKeyboardControls 中的 setActiveField

- (void)setActiveField:(id)activeField
{
    if (activeField != _activeField)
    {
        if ([self.fields containsObject:activeField])
        {
            _activeField = activeField;

            if (![activeField isFirstResponder])
            {
                [activeField becomeFirstResponder];
            }

            [self updateSegmentedControlEnabledStates];
        }
    }
}
4

1 回答 1

4

由于您正在使用带有 UITextFields 的 UIScrollView,因此您可以使用scrollRectToVisibleUIScrollview 的方法,方法大致如下:

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    [_myScrollView scrollRectToVisible:textField.frame animated:YES];
}

为此,您需要确保滚动视图UIViewControllerUITextFieldDelegate的每个文本字段都是 。您可以在 UIViewController 的 viewDidLoad 方法中执行此操作:

[textField1 setDelegate:self];
[textField2 setDelegate:self];

...等等

于 2013-02-05T07:14:19.143 回答