0

我有一个要创建的 UITextField 数组。但 UITextField 保持在同一个框架上?这是我的代码。当您按下按钮时,它会创建一个新的 UITextField。

-(IBAction)textFieldcreating{


    textfieldform = [[NSMutableArray alloc] init];
    textField1 = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 100, 40)];
    textField1.borderStyle = UITextBorderStyleRoundedRect;
    textField1.font = [UIFont systemFontOfSize:15];
    textField1.placeholder = @"enter text";
    textField1.autocorrectionType = UITextAutocorrectionTypeNo;
    textField1.keyboardType = UIKeyboardTypeDefault;
    textField1.returnKeyType = UIReturnKeyDone;
    textField1.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField1.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;    
    textField1.delegate = self;

    [textfieldform addObject:textField1];

    int counter;
    counter = 0;
    if(counter < [textfieldform count])
    {


            textField1.frame = CGRectMake(textField1.frame.origin.x+30, textField1.frame.origin.x+40, textField1.frame.size.width+40, textField1.frame.size.height+50);
            [self.view addSubview:textField1];

        counter++;
    }

}
4

1 回答 1

0

您正在做的是创建一个新数组并保持所有 UITextView 的 xOrigin 和 yOrigin 相同!

做这样的事情。

在 .h 文件中,声明

@property (nonatomic, retain) NSMutableArray *textfieldform;
@property (nonatomic, readwrite) int yOrigin;

在 .m 文件中,在viewDidLoad函数中,

self.textfieldform = [NSMutableArray arrayWithCapacity:0];
yOrigin = 0;

现在,你的函数看起来像这样。

-(IBAction)textFieldcreating{


    //textfieldform = [[NSMutableArray alloc] init]; NO NEED OF THIS LINE NOW
    textField1 = [[UITextField alloc] initWithFrame:CGRectMake(10, yOrigin, 100, 40)];
    textField1.borderStyle = UITextBorderStyleRoundedRect;
    textField1.font = [UIFont systemFontOfSize:15];
    textField1.placeholder = @"enter text";
    textField1.autocorrectionType = UITextAutocorrectionTypeNo;
    textField1.keyboardType = UIKeyboardTypeDefault;
    textField1.returnKeyType = UIReturnKeyDone;
    textField1.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField1.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;    
    textField1.delegate = self;

    [textfieldform addObject:textField1];

    yOrigin = yOrigin + 40 + 10; //old yorigin + btn height + y offset
}

如果您也想更改 xOrigin,请使用 xOrigin 获取另一个变量

于 2012-10-25T03:26:38.817 回答