user2010338
user2010338

Reputation:

Xcode: Passing label to second view?

I have two location labels on my first view controller I need them to be displayed on another view controller when opened, what is the best way to implement this?

The labels i want to pass look like this:

  latitudeLabel.text = [NSString stringWithFormat:@"latitude=%f", location.coordinate.latitude];
longitudeLabel.text = [NSString stringWithFormat:@"longitude=%f", location.coordinate.longitude];

Upvotes: 0

Views: 740

Answers (3)

pbibergal
pbibergal

Reputation: 2921

You have to create identical labels on both view controllers and pass only the string data. You can pass parameters from one class to another with properties (since they are public).

In first view controller .m:

- (void)openSecondViewController {
    SecondViewController *controller = [SecondViewController alloc] init];
    controller.data = [NSArray arrayWithObjects:@"First String", @"Second String", nil];
    [self.navigationController pushViewController:controller];
}


In second view controller .h:

@interface SecondViewController : UIViewController
    @property (weak, nonatomic) id data;
    @property (weak, nonatomic) IBOutlet UILabel *label1;
    @property (weak, nonatomic) IBOutlet UILabel *label2;
@end


In second view controller .m:

- (void)setData:(id)data {
    self.label1.text = [data objectAtIndex:0];
    self.label2.text = [data objectAtIndex:1];
}

Upvotes: 0

ilhnctn
ilhnctn

Reputation: 2210

i had created a sample for sharing data between classes.. All you need to do is explained here

Upvotes: 1

mas'an
mas'an

Reputation: 160

Before you push secondViewController you can add your labels as subviews of second ViewController's view

 SecondViewController *cntr = [[SecondViewController alloc] initWithNibName:@"" bundle:nil];
 [cntr.view addSubview latitudeLabel];
 [cntr.view addSubview longitudeLabel];

 [self.navigationController  pushViewController:cntr animated:YES]

Upvotes: 0

Related Questions