2013-07-18 55 views
0

非常非常新的iOS編程,所以我感謝任何幫助。 我真正想要做的就是當按下按鈕時,將當前座標的變量傳遞給不同的視圖。我無法解決如何使用我目前的工作做到這一點 - 我陷入了一片混亂。我的項目基本上是由來自各種來源的大量代碼組成的。我會與你分享我認爲相關的部分,希望有人能夠至少指出我正確的方向!傳遞位置到不同的視圖與segue

這是從我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 - 但我的問題是,我根本不知道如何讓我的座標變成這種情況。

我希望有人可以幫我(以及我對大量的複製粘貼代碼道歉!)

+0

備註:與每米計算MKMapPoints相比,使用[MKCoordinateReg ionMakeWithDistance函數](https://developer.apple.com/library/ios/#documentation/MapKit/Reference/MapKitFunctionsReference/Reference/reference.html#//apple_ref/doc/uid/TP40008209-CH2-SW5)並執行setRegion而不是setVisibleRect。 – Anna

+0

我會考慮實現這一點,謝謝你指出。 –

回答

0

要通過使用塞格斯視圖控制器之間的對象,像做了以下內容:

1)在IB中的源視圖控制器和目標視圖控制器之間創建一個segue,給它一個標識符@"MySegue"

2)假設目標VC需要一個字符串來運行:通過初始化屬性

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

4)源VC準備:

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

3)源VC發起SEGUE在目的地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 
    } 
} 
+0

謝謝你的幫助。只是澄清:在[3]中,我應該在哪裏準確地放置這條線? –

+0

如果您直接在故事板中進行遊戲,則不需要第3行。如果您想從非故事板事件中執行segue,那麼該代碼將非常有用。我想@danh只是爲了說明而放在那裏。 – Can

+0

@Can是正確的,因爲IB允許您將segues的起始端附加到按鈕和表格單元格之類的東西上。這些是在用戶按下按鈕或選擇單元格等時執行的,並且不需要第3步。您也可以從一個視圖控制器繪製到另一個視圖控制器,然後按照我在步驟3中建議的方式在代碼中啓動它們。如果你這樣做,你會在任何時候想要發生繼續時調用segue,例如,一旦你接收到位置數據。 – danh

相關問題