简体   繁体   English

无法在屏幕上显示文本字段

[英]Couldn't show textfield on screen

I added a text field in viewDidLoad but it did not show up on screen. 我在viewDidLoad中添加了一个文本字段,但未在屏幕上显示。

Here's .h 这是.h

#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController{
    UITextField *tfText;
}
@property (nonatomic, retain) UITextField *tfText;
@end

Here's .m 这是.m

- (void)viewDidLoad{
    [super viewDidLoad];    

    [self.view setBackgroundColor:[UIColor lightGrayColor]];

    tfText.frame = CGRectMake(65, 100, 200, 50);
    tfText.backgroundColor = [UIColor whiteColor];    
    [tfText setTextColor:[UIColor blackColor]];
    tfText.placeholder = @"Test";
    [tfText setBorderStyle:UITextBorderStyleNone];
    [self.view addSubview:tfText]; 
}

seems that you need to initialize the object... I mean 似乎您需要初始化对象...我的意思是

UITextField *newTextField = [[UITextField alloc] init];
self.tfText = newTextField;
//....
//all your code here
//....
[newTextField release];

And dont forget to release your instance on dealloc method. 并且不要忘记在dealloc方法上释放实例。

D33pN16h7 is correct, you need to instantiate your object. D33pN16h7是正确的,您需要实例化您的对象。 However I would do it a little differently than described above, there is no reason to create a UITextField instance, and then set your instance to it. 但是,我所做的工作与上面描述的有所不同,没有理由创建UITextField实例,然后将其设置为该实例。

self.tfText = [[UITextField alloc] init]; self.tfText = [[UITextField alloc] init];

    UITextField entered2 = [[UITextField alloc] initWithFrame:CGRectMake(120.0, 125.0, 150.0, 25.0)]; 
[entered2 setBackgroundColor:[UIColor whiteColor]]; 
entered2.text=@"Active";    
[self.view addSubview:entered2];
[entered2 release];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM