Pangolin
Pangolin

Reputation: 7444

iPhone Coding: How can I set a variable in a ViewController from another?

I have a Tab Bar Application, with an MKMapView on one tab's-view (FirstView.xib & FirstViewController.h/.m), and a normal view with labels on it on the other tab's-view (SecondView.xib).

I have an active CoreLocation running and updating in my FirstViewController. I need the CoreLocation Variables in on my SecondView as well. It has to display speed, altitude, lat/lon on the SecondView.xib.

How must I approach this? Should I have a ViewController for the SecondView as well? Maybe with another instance of CoreLocation? Or is it better to update the secondview's labels from within FirstViewController? (I don't know how to implement the second option - i.e. controlling two xib's from one controller.)

Thanks in advance!

Upvotes: 1

Views: 215

Answers (2)

dredful
dredful

Reputation: 4388

You should try putting the you CLLocation manager within your tab view controller. Then when it updates, it walks the tab views to see if they answer to a location update method you specify. That way it can handle 1, 2,...n views that need the location information.

In your tab view controller:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{

    for (UIViewController *thisTabView in tabBarController.viewControllers) {
        NSLog(@"thisTabView: %@", [thisTabView description]);
        if ([thisTabView respondsToSelector:@selector(locationManager:didUpdateToLocation:fromLocation:)]) {
            NSLog(@"thisTabView: calling locationManager:didUpdateToLocation:fromLocation:");
            [thisTabView locationManager:manager didUpdateToLocation:newLocation fromLocation:oldLocation];
        }
    }

}

Upvotes: 0

darthwillard
darthwillard

Reputation: 819

all your viewcontrollers have access to your appdelegate, so use that to store variables and pass them back to update labels, etc.

MyAppDelegate* delegate = (MyAppDelegate*)[[UIApplication sharedApplication] delegate];

then you can use that to point to your strings using delegate.someString = blah blah;

Upvotes: 1

Related Questions