Search code examples
objective-cxcode4uitextfieldiboutlet

UITextField: set text from a label


I'm trying to write my first iPad app using Xcode 4: it's based on "Tabbed Application" template with two views. On the first view, user selects a City (label displays selection) and on the second wiew there is a IBOutletCollection(UITextField) NSArray *playersData. I want to set the city selected on first view as a default city on second view. I have checked storyboard connections and they seem to be ok.

I get nothing. Any idea?

First view :
#import

@interface pruebaF1FirstViewController : UIViewController
- (IBAction)selectCity:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UILabel *citySelected;
@end

First view implementation : #import "pruebaF1FirstViewController.h" #import "pruebaF1SecondViewController.h"

@interface pruebaF1FirstViewController ()
@property pruebaF1SecondViewController *secondView;
@end

@implementation pruebaF1FirstViewController
@synthesize citySelected;
@synthesize secondView;

- (void)viewDidLoad
...

- (IBAction)selectCity:(UIButton *)sender {
    NSString *currentCity =[sender currentTitle];
    citySelected.text=@"";
    citySelected.text =[citySelected.text stringByAppendingString:currentCity];

    /*writes null*/
    secondView.defaultCity.text=[NSString stringWithFormat:@"%@" ,currentCity];
    NSLog(@"%@",secondView.defaultCity.text);
}
@end

Second view header

#import <UIKit/UIKit.h>
@interface pruebaF1SecondViewController : UIViewController <UITextFieldDelegate> 

@property (strong, nonatomic) IBOutletCollection(UITextField) NSArray *playersData;
@property (retain, nonatomic) IBOutlet UITextField *defaultCity;

- (IBAction)eraseData:(UIButton *)sender;
- (IBAction)savePlayersData:(UIButton *)sender;

- (IBAction)termsPopUp:(UIButton *)sender;

/*- (void)writeDefaultCity:(NSString *)currentCity;*/
@end

Second view implementation #import "pruebaF1SecondViewController.h" #import "pruebaF1FirstViewController.h"

@interface pruebaF1SecondViewController ()
@property (nonatomic, strong)pruebaF1FirstViewController    *prueba;
@end

@implementation pruebaF1SecondViewController
@synthesize playersData;
@synthesize defaultCity;
@synthesize prueba;

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewDidUnload
{
    [self setPlayersData:nil];
    [self setDefaultCity:nil];
    [super viewDidUnload];
}

/* Return or Done button dismiss keyboard*/
-(BOOL)textFieldShouldReturn:(UITextField *)boxes
{
    for (UITextField *boxes in playersData) {
        [boxes resignFirstResponder];
    }
    return YES;
}

....

/*Trying to get city's name from first view in two ways*/

/*-(void)setDefaultCity
{ 
 NSString *defecto=prueba.citySelected.text;
 self.defaultCity.text = defecto;
}*/
- (IBAction)eraseData:(UIButton *)sender {

    for (UITextField *boxes in playersData) {
        boxes.text = @" ";
        }
}
/*
-(void)writeDefaultCity:(NSString *)currentCity
{       
    defaultCity.text =[NSString stringWithFormat:@"%@" ,currentCity]; 
    NSLog(@"Ciudad elegida: %@",currentCity);
}*/
....
@end

Solution

  • Views are generally not loaded until they are displayed on the device, and may be unloaded at any time when not visible to save memory. This means that when you're setting the 'text' property of the 'defaultCity', that label has not yet been created.

    Instead you should add a NSString * property to pruebaF1SecondViewController and set the value of the defaultCity label in viewDidLoad:

    In the header:

    @property (nonatomic, weak) UILabel *defaultCityLabel;
    @property (nonatomic, strong) NSString *defaultCity;
    

    In the implementation

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        defaultCityLabel.text = defaultCity;
    }
    

    And in the first view controller, assign the string value instead of the label value.