iphone - 以编程方式创建 UITextField iphone 时出现问题

标签 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];

}

有什么线索可以解释为什么他们没有出现吗?

提前致谢。

最佳答案

您似乎从未将 scrollView 添加为 subview 。


您的文本字段实际上是隐藏的,因为您从未设置边框样式。试试这个:

- (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];
}

关于iphone - 以编程方式创建 UITextField iphone 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6371413/

相关文章:

ios - 需要帮助为 ios 中的可拖动对象选择父类

ios - 在 Split View的弹出式 Controller 中以 UIModalPresentationFormSheet 样式呈现的对话框在旋转后消失

android - 在应用程序之间共享私有(private)持久数据的任何解决方案?

iphone - 委托(delegate)和多种方法

ios - 编辑 TextView 属性

objective-c - 使用 objective-c 文件创建 swift cocoa touch 框架

ios - BadgeValue不能在UITabBarItem上更新

objective-c - 如果比较两个 NSString 对象时 "a == b"为假

iphone - 如何在 iOS 中创建时间表?

ios - 无法远程调试简单的 React-Native 项目