开发者

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!


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;


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];
        }
    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜