2013-05-03 84 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