iPhone:显示键盘和方向更改时定位多个UITextFields

时间:2010-03-30 10:59:29

标签: iphone uitextfield

我有一个视图,其中包含以编程方式创建的10个UITextField。我想要以下行为:

  1. 当我点击特定的UITextField时,键盘应该隐藏所选文本字段下方所有文本字段。
  2. 如果我选择了文本字段并更改设备方向,则文本字段和键盘都应旋转到正确的方向,而文本字段不会丢失选择焦点。
  3. 我需要控制键盘中的返回键是否处于活动状态。
  4. 如何管理这些文本字段以获取这些行为。

1 个答案:

答案 0 :(得分:0)

在scrollview中添加文本字段并为所有文本字段设置标记。然后在您的应用程序中放入以下代码。您必须根据您的要求输入文本字段位置。

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
// Begin animations to move TextFields into view.

if (textField.tag == 1) {

    [UIView beginAnimations: @"moveField" context: nil];
    [UIView setAnimationDelegate: self];
    [UIView setAnimationDuration: 0.5];
    [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
    self.scrlview.frame = CGRectMake(0,30,320,357);
    [UIView commitAnimations];
    textfield2.hidden=YES;
    textfield3.hidden=YES;
    textfield4.hidden=YES;



}     

else if(textField.tag == 2)
{

    [UIView beginAnimations: @"moveField" context: nil];
    [UIView setAnimationDelegate: self];
    [UIView setAnimationDuration: 0.5];
    [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
    self.scrlview.frame = CGRectMake(0,30,320,357);
    [UIView commitAnimations];
    textfield1.hidden=YES;
    textfield3.hidden=YES;
    textfield4.hidden=YES;


}

else if(textField.tag == 3)
{

    [UIView beginAnimations: @"moveField" context: nil];
    [UIView setAnimationDelegate: self];
    [UIView setAnimationDuration: 0.5];
    [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
    self.scrlview.frame = CGRectMake(0,25,320,357);
    [UIView commitAnimations];
    textfield1.hidden=YES;
    textfield2.hidden=YES;
    textfield4.hidden=YES;


}

else if(textField.tag == 4)
{

    [UIView beginAnimations: @"moveField" context: nil];
    [UIView setAnimationDelegate: self];
    [UIView setAnimationDuration: 0.5];
    [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
    self.scrlview.frame = CGRectMake(0,20,320,357);
    [UIView commitAnimations];
    textfield1.hidden=YES;
    textfield2.hidden=YES;
    textfield3.hidden=YES;


}  


return YES;

}

//Set the objects on the view when device orientation will change.
 -(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration {

if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation ==UIInterfaceOrientationLandscapeRight) {

    // set the views oreintation here for landscapemode.        
}
if(interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown ||
   interfaceOrientation == UIInterfaceOrientationPortrait) {


    //set the views oreintation here for Portraitmode.
}

}

//当方向改变时调用委托

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  {
// Return YES for supported orientations
return YES;

}