无法在其他类中获取float变量的值

时间:2014-07-18 05:44:48

标签: objective-c

我是Objective-C的新手,但我确实理解OO编程。现在,我有这个课程,它获取当前位置的经度和纬度。打印出来很好。当我尝试从另一个类访问它时出现问题。当我在另一个类中调用getLatitude时,它一直给我0.000000的lat和long,我真的不知道如何解决它。我把变量公之于众,并且有吸气剂和制定者。这应该是一个简单的解决方案,但我对O-C很新。请帮助......我已经坚持好几天了。谢谢。

TableViewController.h

@interface TableViewController : UITableViewController{
    LocationSearchViewController *locationSearch;
}
@end

TableViewController.m

@interface TableViewController ()

@property (strong, nonatomic) NSArray *googlePlacesArrayFromAFNetworking;

@end

@implementation TableViewController

-(void)makeRestuarantRequests
{
    locationSearch = [[LocationSearchViewController alloc] init];

    NSString *longitude = [NSString stringWithFormat:@"%f", locationSearch->longitude];
    NSString *latitude = [NSString stringWithFormat:@"%f", [locationSearch getLatitude]];

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/textsearch/json?location=%@,%@&radius=100&query=restuarants&sensor=false&key=KEY", latitude, longitude]];

    NSLog(@"Long %@", longitude); ---> gives 0.000000
    NSLog(@"Lat %@", latitude);  ---> gives 0.000000
       .
       .
       .
    [operation start];
    }

LocationSearchViewController.h

@interface LocationSearchViewController : UIViewController<CLLocationManagerDelegate>{

@public float longitude;
@public float latitude;
}

@property (weak, nonatomic) IBOutlet UILabel *latitudeLabel;
@property (weak, nonatomic) IBOutlet UILabel *longitudeLabel;
//@property (nonatomic, readwrite) float longitude;
//@property (nonatomic, readwrite) float latitude;

- (IBAction)currentLoc:(id)sender;
- (float) getLatitude;
- (float) getLongitude;
-(void) setLatitude:(float)latitude;
-(void) setLongitude:(float)longitude;

@end

LocationSearchViewController.m

@interface LocationSearchViewController ()
 @end

@implementation LocationSearchViewController {
      CLLocationManager *locationManager;
}

@synthesize longitudeLabel = _longitudeLabel;
@synthesize latitudeLabel = _latitudeLabel;

- (void)viewDidLoad
{
    [super viewDidLoad];
    locationManager = [[CLLocationManager alloc] init];
}

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

- (IBAction)currentLoc:(id)sender { 
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
}
    .
    .
    .

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

    NSLog(@"didUpdateLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

   [self setLongitude:currentLocation.coordinate.longitude];
    [self setLatitude:currentLocation.coordinate.latitude];
  //longitude = currentLocation.coordinate.longitude;
    //latitude = currentLocation.coordinate.latitude;

    if(currentLocation != nil) {
        //returns current latitude and longitude
        self.longitudeLabel.text = [NSString stringWithFormat:@"%.6f",  longitude];
        self.latitudeLabel.text = [NSString stringWithFormat:@"%.6f",  latitude];
    } 
    [locationManager stopUpdatingLocation];
}


- (float) getLatitude{
    return latitude;
    }

- (float ) getLongitude{
    return longitude;
}

-(void) setLatitude:(float)lati{
    latitude = lati;
}

-(void) setLongitude:(float)longti{
    longitude=longti;
}

@end

2 个答案:

答案 0 :(得分:0)

我不确定您的应用程序的流程,但我认为您在TableViewController中创建了一个新的LocationSearchViewController实例,这就是为什么您获得0.000的价值。

您能告诉我们您应用的流程,以便能够正确理解您的问题吗?

我建议使用委托模式,以便您可以通过纬度和范围。在LocationSearchViewController中收到经度到TableViewController。看一下详细描述设计模式的link

答案 1 :(得分:0)

LocationSearchViewController的locationManger将在调用currentLoc:后更新位置。在makeRestuarantRequests中,您实例化LocationSearchViewController,但currentLoc:看起来不会被调用。

调用currentLoc时,可以在调用委托方法-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation后获取坐标值。