UITextField 控件丢失委托引用 ID
UITextField control losing delegate reference ID
我正在通过代码(不是 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);
在viewWillAppear
// 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 打印出来。请注意,对象具有 pointer/reference 编号 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 对象下的导航器 window 中,条目 _delegate = (id) 0x0 在 viewWillAppear 中断后出现,但显示了 tfOAT 的参考编号。两个控件仍在屏幕上,但 tfWind 已变为 "disconnected".
如果我取消注释 //[tfWind setText:@"1"]; viewDidLoad 中的行(重新启动应用程序),我确实让“1”出现在文本字段中。
这真是一个谜。有人有任何想法或以前看过吗?
啊...
它具有 "become disconnected" 的原因是因为您正在 viewDidLoad()
中创建一个新变量:
- (void)viewDidLoad {
[super viewDidLoad];
// creates a text field object for the class property tfOAT
tfOAT = [[UITextField alloc] initWithFrame:frame];
...
// creates a NEW LOCAL text field object
UITextField *tfWind = [[UITextField alloc] initWithFrame:frame];
...
// end of viewDidLoad, the LOCAL tfWind goes out-of-scope
}
看出区别了吗?
我正在通过代码(不是 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);
在viewWillAppear
// 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 打印出来。请注意,对象具有 pointer/reference 编号 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 对象下的导航器 window 中,条目 _delegate = (id) 0x0 在 viewWillAppear 中断后出现,但显示了 tfOAT 的参考编号。两个控件仍在屏幕上,但 tfWind 已变为 "disconnected".
如果我取消注释 //[tfWind setText:@"1"]; viewDidLoad 中的行(重新启动应用程序),我确实让“1”出现在文本字段中。
这真是一个谜。有人有任何想法或以前看过吗?
啊...
它具有 "become disconnected" 的原因是因为您正在 viewDidLoad()
中创建一个新变量:
- (void)viewDidLoad {
[super viewDidLoad];
// creates a text field object for the class property tfOAT
tfOAT = [[UITextField alloc] initWithFrame:frame];
...
// creates a NEW LOCAL text field object
UITextField *tfWind = [[UITextField alloc] initWithFrame:frame];
...
// end of viewDidLoad, the LOCAL tfWind goes out-of-scope
}
看出区别了吗?