0
votes

I am adding controls to a ViewController by code (not XIB).

Form has two UITextfield controls which are defined as IBOutlet in the .h file

@interface ConditionsViewController : UIViewController <UITabBarDelegate, UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource>
{
id tfDelegate;  
IBOutlet UITextField *tfOAT;
IBOutlet UITextField *tfWind;
}
- (void) setTextFieldAttributes:(UITextField *)tf;
...

In the .m within viewDidLoad, both UITextField are init and 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);

In the 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);

Here is the setTextAttributes function, doesn't do anything special.

- (void) setTextFieldAttributes:(UITextField *)tf
{

    tf.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    tf.borderStyle = UITextBorderStyleRoundedRect;
    [tf setDelegate:self];
    tf.clearsOnBeginEditing = YES;
    [tf setKeyboardType:UIKeyboardTypeNumbersAndPunctuation];
    [tf setReturnKeyType:UIReturnKeyDone];

}

The NSLog prints out as such. Note that the objects have pointer/reference numbers 0x7fccad7219a0 and 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)

Note that the pointer or reference to tfWind has vanished somewhere between viewDidLoad and viewWillAppear. In the navigator window under the tfWind object, the entry _delegate = (id) 0x0 appears after a break in viewWillAppear but shows the reference number for tfOAT. Both controls are still on the screen but tfWind has become "disconnected".

If I uncomment the //[tfWind setText:@"1"]; line in the viewDidLoad (restart the app), I indeed do get the "1" to appear within the textfield.

This is a real mystery. Anyone have any ideas or seen this before?

1
First note... IBOutlet UITextField *tfWind; indicates you've added and connected this via IB / Storyboard. You shouldn't be creating a new instance of tfWind in viewDidLoad because it should already exist. If you are not connecting it in IB, why is it marked IBOutlet?DonMag

1 Answers

0
votes

Ah...

The reason it has "become disconnected" is because you are creating a new variable in 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
}

See the difference?