【发布时间】:2020-09-05 23:33:54
【问题描述】:
我正在通过代码(不是 XIB)向 ViewController 添加控件。
Form 有两个 UITextfield 控件,它们在 .h 文件中定义为 IBOutlet
@interface ConditionsViewController : UIViewController <UITabBarDelegate, UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource>
{
id tfDelegate;
IBOutlet UITextField *tfOAT;
IBOutlet UITextField *tfWind;
}
- (void) setTextFieldAttributes:(UITextField *)tf;
...
在viewDidLoad中的.m中,UITextField都是init和alloc
[super viewDidLoad];
tfDelegate = self;
//create OAT textfield
frame = CGRectMake( xRightSide-boxWidth*1.5-5, 200, boxWidth*0.75, boxHeight );
tfOAT = [[UITextField alloc] initWithFrame:frame];
[tfOAT setFont:font];
[self setTextFieldAttributes:tfOAT];
[[self view] addSubview:tfOAT];
NSLog(@"OAT after addSubview is: %@",tfOAT);
//create Wind textfield
frame = CGRectMake( xRightSide-boxWidth*1.5-5, 400, boxWidth*0.75, boxHeight );
UITextField *tfWind = [[UITextField alloc] initWithFrame:frame];
[tfWind setFont:font];
[self setTextFieldAttributes:tfWind];
//[tfWind setText:@"1"];
[[self view] addSubview:tfWind];
NSLog(@"WindField after addSubview is: %@",tfWind);
在视图中会出现
// OAT is returned
float t = [pModel OAT]; //t returns 12
if( t == 0 )
[tfOAT setText:@""];
else
[tfOAT setText:[NSString stringWithFormat:@"%.0f", t]];
NSLog(@"OAT within viewWillAppear is: %@",tfOAT);
NSLog(@"OAT text is: %@",tfOAT.text);
// Wind component is returned
int w = [pModel wind]; //w does return an integer 8
if(w==0)
[tfWind setText:@"0"];
else
[tfWind setText:[NSString stringWithFormat:@"%d",w]];
NSLog(@"WindField in viewDidAppear: %@",tfWind);
NSLog(@"Windfield text is: %@",tfWind.text);
这里是setTextAttributes函数,没什么特别的。
- (void) setTextFieldAttributes:(UITextField *)tf
{
tf.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
tf.borderStyle = UITextBorderStyleRoundedRect;
[tf setDelegate:self];
tf.clearsOnBeginEditing = YES;
[tf setKeyboardType:UIKeyboardTypeNumbersAndPunctuation];
[tf setReturnKeyType:UIReturnKeyDone];
}
NSLog 会这样打印出来。请注意,对象的指针/引用号为 0x7fccad7219a0 和 0x7fccb041beb0。
//NSLogs within viewDidLoad
OAT after addSubview is: <UITextField: 0x7fccad7219a0; frame = (510.5 200; 71.25 55); text = ''; opaque = NO; layer = <CALayer: 0x60000328f0e0>>
WindField after addSubview is: <UITextField: 0x7fccb041beb0; frame = (510.5 400; 71.25 55); text = ''; opaque = NO; layer = <CALayer: 0x600003284860>>
//NSLogs within viewWillAppear
OAT within viewWillAppear is: <UITextField: 0x7fccad7219a0; frame = (510.5 200; 71.25 55); text = '12'; opaque = NO; tag = 3; layer = <CALayer: 0x60000328f0e0>>
OAT text is:12
WindField within viewDidAppear: (null)
Windfield text is: (null)
请注意,指向 tfWind 的指针或引用在 viewDidLoad 和 viewWillAppear 之间的某处消失了。在 tfWind 对象下的导航器窗口中,条目 _delegate = (id) 0x0 在 viewWillAppear 中断后出现,但显示了 tfOAT 的参考编号。两个控件仍在屏幕上,但 tfWind 已“断开连接”。
如果我取消注释 //[tfWind setText:@"1"]; viewDidLoad 中的行(重新启动应用程序),我确实让“1”出现在文本字段中。
这是一个真正的谜。有人有任何想法或以前见过吗?
【问题讨论】:
-
第一个注释...
IBOutlet UITextField *tfWind;表示您已通过 IB / Storyboard 添加并连接了它。您不应该在viewDidLoad中创建tfWind的new 实例,因为它应该已经存在。如果您没有在 IB 中连接它,为什么它会被标记为IBOutlet?
标签: ios objective-c xcode uitextfield