2013-05-03 86 views
1

嗨,我可以很容易地滚动到一个文本框的工作,但是当我添加10个文本框,并使用苹果文档中的代码我无法弄清楚如何让它委托触摸任何10个字段中的任何一个滚动视图,我无法解决如何将activeField连接到相关的textField。我想在那里IM飘落下来,多数民众赞成,而答案就在代表团多个文本字段的滚动视图不会工作

@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)]; 

} 

//在您的视图控制器设置代码某处调用此方法。 - (无效)registerForKeyboardNotifications { [[NSNotificationCenter defaultCenter]的addObserver:自选择器:@selector(keyboardWasShown :) 名:UIKeyboardDidShowNotification对象:无];

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

}

//当发送UIKeyboardDidShowNotification调用。 (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]; 
} 

}

//当UIKeyboardWillHideNotification发送 调用 - (空)keyboardWillBeHidden:(NSNotification *)aNotification {

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

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

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

回答

1

您可以管理使用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]; 
    } 

} 
+0

你有没有试过这个@Imran Raja? – Balu 2013-05-03 10:04:50

+0

每个文本框都需要一个标签以符合所提到的标签属性,但是这个代码字会对cgpointmake值进行一些较小的调整,因此它向上滚动了第10个textField的正确数量,这里没有提到,谢谢我帮助 – 2013-05-03 16:35:05

+0

是的Ofcorse我的意思是在上面的代码中替换你的值。 – Balu 2013-05-04 04:26:34