iphone以编程方式创建UITextField的问题

时间:2011-06-16 11:58:11

标签: iphone objective-c cocoa-touch uitextfield

当我尝试以编程方式创建11个文本字段时,我正在努力。问题是文本字段没有显示出来。我正在viewDidLoad方法中创建它们。

这是我正在使用的代码:

- (void)viewDidLoad
{
    // Determine some basic info
    int numberOfTextfields = 11;
    int textfieldHeight = 40;
    int textfieldWidth = 200;


    // Create the UIScrollView
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, numberOfTextfields*textfieldHeight,textfieldWidth)];


    // Create all the textfields
    NSMutableArray *textfields = [NSMutableArray arrayWithCapacity:
                                  (NSUInteger)numberOfTextfields];
    for(int i = 0; i < numberOfTextfields; i++) {
        UITextField *field = [[UITextField alloc] initWithFrame:
                              CGRectMake(0,i*textfieldHeight,textfieldHeight,textfieldWidth)];

        [scrollView addSubview:field];
        [textfields addObject:field];
    }

    [super viewDidLoad];

}

有关他们为什么不出现的任何线索?

提前致谢。

5 个答案:

答案 0 :(得分:5)

您似乎永远不会将scrollView添加为子视图。


您的textFields通常是隐藏的,因为您从未设置边框样式。试试这个:

- (void)viewDidLoad
{
    // Determine some basic info
    int numberOfTextfields = 11;
    int textfieldHeight = 40;
    int textfieldWidth = 200;


    // Create the UIScrollView
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, textfieldWidth,numberOfTextfields*textfieldHeight)];
    scrollView.contentSize = CGSizeMake(numberOfTextfields*textfieldWidth, textfieldHeight);

    // Create all the textfields
    NSMutableArray *textfields = [NSMutableArray arrayWithCapacity:
                                  (NSUInteger)numberOfTextfields];
    for(int i = 0; i < numberOfTextfields; i++) {
        UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(0,i*textfieldHeight,textfieldWidth,textfieldHeight)];
        field.borderStyle = UITextBorderStyleRoundedRect;
        [scrollView addSubview:field];
        [textfields addObject:field];
    }

    [self.view addSubview:scrollView];

    [super viewDidLoad];
}

答案 1 :(得分:0)

您需要将UIScrollView添加到视图以设置其内容区域,因为它不会自动管理它(如您所料)。

答案 2 :(得分:0)

您需要将滚动视图添加到视图控制器。如果分配,
否则这样做 self.view = [[UIView alloc] initWithFrame:CGRectMake (0,0,320,460)] 在最后[self.view addSubview: scrollView]

添加此行

答案 3 :(得分:0)

检查此代码.....帧参数是(x,y,宽度,高度)

    int numberOfTextfields = 11;
    int textfieldHeight = 40;
    int textfieldWidth = 200;


    // Create the UIScrollView
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,textfieldWidth,numberOfTextfields*textfieldHeight)];


    // Create all the textfields
    NSMutableArray *textfields = [NSMutableArray arrayWithCapacity:
                                  (NSUInteger)numberOfTextfields];
    for(int i = 0; i < numberOfTextfields; i++) {
        UITextField *field = [[UITextField alloc] initWithFrame:
                              CGRectMake(0,i*textfieldHeight,textfieldWidth,textfieldHeight)];
        field.borderStyle=UITextBorderStyleRoundedRect;
        [scrollView addSubview:field];
        [textfields addObject:field];
    }
    [self.view addSubview:scrollView];
    [super viewDidLoad];

答案 4 :(得分:0)

尝试以下代码。添加了文本字段,您可以滚动文本视图。

int numberOfTextfields = 11;
int textfieldHeight = 40;
int textfieldWidth = 200;

UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,textfieldWidth,numberOfTextfields*textfieldHeight)]; scrollView.contentSize = CGSizeMake(textfieldWidth, numberOfTextfields*textfieldWidth+10);

NSMutableArray *textfields = [NSMutableArray arrayWithCapacity: (NSUInteger)numberOfTextfields];

for(int i = 1; i < numberOfTextfields; i++) { UITextField *field = [[UITextField alloc] initWithFrame: CGRectMake(0,i*textfieldHeight,textfieldWidth,textfieldHeight)]; field.borderStyle=UITextBorderStyleRoundedRect; [scrollView addSubview:field]; [textfields addObject:field]; }

[self.view addSubview:scrollView]; [super viewDidLoad];