专注于UITextField不起作用

时间:2013-02-05 07:05:15

标签: iphone ios xcode


在我的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];
        }
    }
}

1 个答案:

答案 0 :(得分:4)

由于您正在使用带UITextFields的UIScrollView,您可以使用scrollRectToVisible方法进行UIScrollview,方法大致如下:

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

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

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

......等等