Scroll view for multiple textFields wont work

Hi i can get scroll to work with one text box quite easily, however when i add 10 text boxes, and use code from apples documentation i cant figure out how to get it to delegate the touch of any of the 10 fields to scroll the view, i cant work out how to connect activeField to the textField in question. i think thats where im falling down, and the answer lies in delegation

@interface ImmyViewController ()

@end

@implementation ImmyViewController
@synthesize activeField;
@synthesize scrollView;
@synthesize text1;
@synthesize text2;
@synthesize text3;
@synthesize text4; 
@synthesize text5;
@synthesize text6;
@synthesize text7;
@synthesize text8;
@synthesize text9;
@synthesize text10;

- (void)viewDidLoad
{
    [super viewDidLoad];

    text1.delegate =self;
    text2.delegate =self;
    text3.delegate =self;
    text4.delegate =self;
    text5.delegate =self;
    text6.delegate =self;
    text7.delegate =self;
    text8.delegate =self;
    text9.delegate =self;
    activeField.delegate=self;

    text10.delegate =self;

// Do any additional setup after loading the view, typically from a nib.
//---set the viewable frame of the scroll view---
    scrollView.frame = CGRectMake(0, 0, 320, 460);

//---set the content size of the scroll view---
    [scrollView setContentSize:CGSizeMake(320, 833)];

}

// Call this method somewhere in your view controller setup code. - (void)registerForKeyboardNotifications { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillBeHidden:)
                                             name:UIKeyboardWillHideNotification object:nil];

}

// Called when the UIKeyboardDidShowNotification is sent. - (void)keyboardWasShown:(NSNotification*)aNotification { NSDictionary* info = [aNotification userInfo]; CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;

// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
    CGPoint scrollPoint = CGPointMake(0.0, text10.frame.origin.y-kbSize.height);
    [scrollView setContentOffset:scrollPoint animated:YES];
}

}

// Called when the UIKeyboardWillHideNotification is sent - (void)keyboardWillBeHidden:(NSNotification*)aNotification {

    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;
 }

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    activeField = textField;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    activeField = nil;
}

您可以使用textFieldDidBeginEditing开始管理所有文本字段,只需在此代码中替换您的值即可。

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    if (textField.tag==1) {
        [scroll_view setContentOffset:CGPointMake(0, 0)animated:YES];
    }
    if (textField.tag==2) {
        [scroll_view setContentOffset:CGPointMake(0, 81)animated:YES]; 
    }
    if (textField.tag ==3) {
        [scroll_view setContentOffset:CGPointMake(0, 115)animated:YES]; 
    }
    if (textField.tag ==4) {
        [scroll_view setContentOffset:CGPointMake(0, 150)animated:YES]; 
    }
    if (textField.tag ==5) {
        [scroll_view setContentOffset:CGPointMake(0, 185)animated:YES]; 
    }
    if (textField.tag ==6) {
        [scroll_view setContentOffset:CGPointMake(0, 220)animated:YES]; 
    }

}
链接地址: http://www.djcxy.com/p/66722.html

上一篇: UITextField textFieldDidBeginEditing:在第一次选择时未调用

下一篇: 滚动视图为多个文本字段不会工作