0

iOS 编程非常非常新,所以我很感激这方面的任何帮助。我真正想做的就是在按下按钮时将当前坐标的变量传递给不同的视图。我无法用我目前的工作弄清楚如何做到这一点——我完全陷入了混乱。我的项目本质上是由来自各种来源的大量代码组成的。我将与您分享我认为相关的部分,希望有人至少可以为我指明正确的方向!

这是来自我的 MainViewController.m:

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

if (currentPosition == nil) {

    MKMapPoint point = MKMapPointForCoordinate(newLocation.coordinate);

    double pointsPerMeter = MKMapPointsPerMeterAtLatitude(newLocation.coordinate.latitude);


    double visibleDistance = pointsPerMeter * 500.0;

    MKMapRect rect = MKMapRectMake(
                                   point.x - visibleDistance, point.y - visibleDistance,
                                   visibleDistance * 2, visibleDistance * 2);
           [self.mapView setVisibleMapRect:rect animated:YES];



    NSURL *url = [NSURL URLWithString:@"https://url.url/json.json"];
    NSData *data = [NSData dataWithContentsOfURL:url];


    NSError *error;
    NSArray *array = [NSJSONSerialization JSONObjectWithData:data
                                                     options:0
                                                       error:&error];
    if (error != nil)
    {

    }


    CLLocationCoordinate2D location;                         
    NSMutableArray *newAnnotations = [NSMutableArray array]; 
    MKPointAnnotation *newAnnotation;                        


    for (NSDictionary *dictionary in array)
    {


        location.latitude = [dictionary[@"lat"] doubleValue];
        location.longitude = [dictionary[@"lon"] doubleValue];


        newAnnotation = [[MKPointAnnotation alloc] init];

        newAnnotation.coordinate = location;



        HUWMapAnnotation *annotation = [[HUWMapAnnotation alloc] initWithCoordinate:newAnnotation.coordinate];
        annotation.messagetitle = dictionary[@"name"];
        annotation.email = dictionary[@"message"];
        annotation.username = dictionary[@"user"];

        [self.mapView addAnnotation:annotation];

        [newAnnotations addObject:newAnnotation];

    }




}
currentPosition = newLocation;
}

- (void)viewDidLoad { [super viewDidLoad];

// Check if the user has enabled location services.
if ([CLLocationManager locationServicesEnabled]) {
    // Create a location manager.
    locationManager = [[CLLocationManager alloc] init];
    // Set ourselves as it's delegate so that we get notified of position updates.
    locationManager.delegate = self;
    // Set the desired accuracy.
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    // Start tracking.
    [locationManager startUpdatingLocation];
}
}

就像我说的,我已将其大幅缩减为我认为相关的部分。如果我错过了一些重要的东西,请告诉我。我已经设置好我的故事板,并准备好一个按钮,以及一个带有标识符的 segue。

我认为我应该使用prepareForSegue- 但我的问题是我根本不知道如何让我的坐标进入那种情况。

我希望有人能够帮助我(对于大量复制粘贴的代码,我深表歉意!)

4

1 回答 1

0

要使用 segue 在视图控制器之间传递对象,请执行以下操作:

1)在IB中的源视图控制器和目标视图控制器之间创建一个segue,给它一个标识符@"MySegue"

2)假设目标vc需要一个字符串来运行:

// DestinationVC.h
@interface DestinationVC : UIViewController
@property(strong, nonatomic) NSString *string;
@end

3)源vc发起一个segue:

[self performSegueWithIdentifier:@"MySegue" sender:self];

4) 源 vc 通过初始化目标 vc 上的属性来准备:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:@"MySegue"]) {
        DestinationVC *vc = [segue destinationViewController];
        vc.string = // some object that the source vc has.  this could be your CLLocationCoordinate2D
    }
}
于 2013-07-18T16:47:04.357 回答