使用segue将位置传递到不同的视图

时间:2013-07-18 16:33:49

标签: iphone ios objective-c

非常非常新的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 - 但我的问题是我根本不知道如何让我的坐标进入那种情况。

我希望有人能够帮助我(我为大量的复制粘贴代码道歉!)

1 个答案:

答案 0 :(得分:0)

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

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