Trying to find which text field is active ios

后端 未结 7 2320
Happy的楠姐
Happy的楠姐 2020-12-28 14:26

I am trying to find which textfield is active for when the I move the view when the keyboard rises. I am trying to set a property in my viewcontroller from the subview of a

7条回答
  •  时光说笑
    2020-12-28 15:01

    I first used Xilexio's solution but it was slow. I ended up using tags. Here is my code and set up as an example.

    @property (nonatomic) NSInteger currentFormField;
    
    typedef NS_ENUM(NSInteger, IOUFormField) {
        IOUFormFieldName,
        IOUFormFieldAmount,
        IOUFormFieldDescription,
        IOUFormFieldDate
    };
    

    ...

    self.nameField.tag = IOUFormFieldName;
    self.amountField.tag = IOUFormFieldAmount;
    self.descriptionField.tag = IOUFormFieldDescription;
    self.dateField.tag = IOUFormFieldDate;
    
    -(void)keyboardWillShow:(NSNotification *)notification {
        // Move the scroll view to a position where the user can see the top and bottom form fields
        // For example, if the user is on the description field, they should be able to see the date field and the amount field.
    
        // The keyboard rect value comes as a NSValue * (a wrapped NSRect) with origin and size.
        // The origin is using screen coordinates which is pixel based so don't use it.
        // Use the size. Seems like it is density based.
        CGFloat viewableScreenHeight = self.view.frame.size.height - keyboardFrameBeginRect.size.height;
    
        // When the user is on a form field, get the current form field y position to where the scroll view should move to
        CGFloat currentFormFieldYPosition = 0;
        switch (self.currentFormField) {
            case IOUFormFieldName:
            {
                currentFormFieldYPosition = self.nameField.frame.origin.y;
    
                // If the scroll view is at the bottom and the user taps on the name field, move the scroll view to the top.
                // This is so that users can see the give/get segments.
                [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    
                break;
            }
            case IOUFormFieldAmount:
            {
                currentFormFieldYPosition = self.amountField.frame.origin.y;
                break;
            }
            case IOUFormFieldDescription:
            {
                currentFormFieldYPosition = self.descriptionField.frame.origin.y;
                break;
            }
            case IOUFormFieldDate:
            {
                currentFormFieldYPosition = self.dateField.frame.origin.y;
                break;
            }
            default:
                break;
        }
    
        // I want the current form field y position to be 100dp from the keyboard y position.
        // 50dp for the current form field to be visible and another 50dp for the next form field so users can see it.
        CGFloat leftoverTopHeight = viewableScreenHeight - 100;
    
        // If the current form field y position is greater than the left over top height, that means that the current form field is hidden
        // We make the calculations and then move the scroll view to the right position
        if (currentFormFieldYPosition > leftoverTopHeight) {
            CGFloat movedScreenPosition = currentFormFieldYPosition - leftoverTopHeight;
            [self.scrollView setContentOffset:CGPointMake(0, movedScreenPosition) animated:YES];
        }   
    }
    
    #pragma mark - UITextFieldDelegate
    
    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
        switch (textField.tag) {
            case IOUFormFieldName:
                self.currentFormField = IOUFormFieldName;
                break;
            case IOUFormFieldAmount:
                self.currentFormField = IOUFormFieldAmount;
                break;
            case IOUFormFieldDescription:
                self.currentFormField = IOUFormFieldDescription;
                break;
            case IOUFormFieldDate:
                self.currentFormField = IOUFormFieldDate;
            default:
                break;
        }
    
        return true;
    }
    

    Let me know if you have any questions and I'll clarify. Note that the comments are for me. Also note that some code or omitted for brevity.

提交回复
热议问题