1

此代码绘制多个文本字段并分配一个标签并将标签编号写入每个字段。如何在标签号为 15 的文本字段中写一个“X”?

- (void)viewDidLoad
{
    int z = 0;
    [super viewDidLoad];
    for (int y = 10; y < 300; y = y + 25)
    {
        for (int x = 10; x < 300; x = x + 25)
        {
            textField = [[UITextField alloc] initWithFrame:CGRectMake(x, y, 25, 25)];
            textField.borderStyle = UITextBorderStyleLine;
            textField.textColor = [UIColor blackColor];
            textField.font = [UIFont fontWithName:@"ArialMT" size:16];
            textField.backgroundColor = [UIColor whiteColor];
            textField.autocorrectionType = UITextAutocorrectionTypeNo;
            textField.backgroundColor = [UIColor clearColor];
            textField.textAlignment = NSTextAlignmentCenter;
            textField.enabled = NO;            
            textField.text = [NSString stringWithFormat:@"%d", z];
            [self.view addSubview:textField];
            textField.tag = z++;
        }
    }
}
4

1 回答 1

0

您可以通过调用获得正确的文本字段viewWithTag

UITextField *myField = (UITextField*)[self.view viewWithTag:15];
// Now that I have my field, I can write some text into it:
myField.text = @"X";
于 2012-12-04T02:10:44.153 回答