2016-11-20 56 views
0

我正在Xcode(objective-c)中開發一個應用程序。我的應用程序有一個帶有餐廳列表的TableView,當您按下一行時,另一個視圖將隨餐廳信息一起打開。我正在使用的方法是,我將行中的標題發送到新視圖,並根據標題加載餐廳的信息。 我想要使用地圖別針按鈕完全相同。我有一個地圖與針腳,我想要按右側的詳細信息按鈕打開另一個視圖與餐廳的信息取決於mapPin標題。使用mapPin按鈕打開一個新視圖Xcode

有人可以幫助我!我正在尋找教程和其他職位,但它不適用於我的情況。非常感謝你!

這是我MapViewController.h:

#import <UIKit/UIKit.h> 
#import <MapKit/MapKit.h> 

@interface MapViewController : UIViewController { 

    MKMapView *mapView; 
} 

@property (weak, nonatomic) IBOutlet UIBarButtonItem *barButton; 

@property (nonatomic, retain) IBOutlet MKMapView *mapView; 

-(IBAction)setMap:(id)sender; 

@end 

這是我MapViewController.m:

#import "MapViewController.h" 
#import "SWRevealViewController.h" 
#import "RestMapPin.h" 
#import "RestViewController.h" 

@interface MapViewController() 

@end 

@implementation MapViewController 

@synthesize mapView; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    _barButton.target = self.revealViewController; 
    _barButton.action = @selector(revealToggle:); 

    [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer]; 

    [self.navigationItem setTitle:NSLocalizedString (@"Map", nil)]; /*Cambia el titulo del navigation controller*/ 

    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}]; /*Cambia el color de las letras del navigation controller bar del menu principal*/ 

    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:27/255.0f green:101/255.0f blue:163/255.0f alpha:1.0f]]; 

    self.navigationController.navigationBar.tintColor = [UIColor whiteColor]; /*Cambia el color del boton de la izquierda*/ 

    mapView.delegate = self; 

    //1 
    //80 Grados 
    MKCoordinateRegion region_80_grados = { {0.0, 0.0}, {0.0, 0.0}}; 
    region_80_grados.center.latitude = 40.42871179999999; 
    region_80_grados.center.longitude = -3.703639100000032; 
    region_80_grados.span.longitudeDelta = 0.1f; 
    region_80_grados.span.latitudeDelta = 0.1f; 
    [mapView setRegion:region_80_grados animated:YES]; 

    RestMapPin *ann_80_grados = [[RestMapPin alloc] init]; 
    ann_80_grados.title = @"80 Grados"; 
    ann_80_grados.subtitle = @"Malasaña"; 
    ann_80_grados.coordinate = region_80_grados.center; 
    [mapView addAnnotation:ann_80_grados]; 

    //90 Grados 
    MKCoordinateRegion region_90_grados = { {0.0, 0.0}, {0.0, 0.0}}; 
    region_90_grados.center.latitude = 40.4164161; 
    region_90_grados.center.longitude = -3.6699459999999817; 
    region_90_grados.span.longitudeDelta = 0.1f; 
    region_90_grados.span.latitudeDelta = 0.1f; 
    [mapView setRegion:region_90_grados animated:YES]; 

    RestMapPin *ann_90_grados = [[RestMapPin alloc] init]; 
    ann_90_grados.title = @"90 Grados"; 
    ann_90_grados.subtitle = @"Retiro"; 
    ann_90_grados.coordinate = region_90_grados.center; 
    [mapView addAnnotation:ann_90_grados]; 

    /*B&B Babel*/ 
    MKCoordinateRegion region_babel = { {0.0, 0.0}, {0.0, 0.0}}; 
    region_babel.center.latitude = 40.4214535; 
    region_babel.center.longitude = -3.6974301; 
    region_babel.span.longitudeDelta = 0.1f; 
    region_babel.span.latitudeDelta = 0.1f; 
    [mapView setRegion:region_babel animated:YES]; 

    RestMapPin *ann_babel = [[RestMapPin alloc] init]; 
    ann_babel.title = @"B&B Babel"; 
    ann_babel.subtitle = @"Barrio de Chueca"; 
    ann_babel.coordinate = region_babel.center; 
    [mapView addAnnotation:ann_babel]; 

    /*Babelia*/ 
    MKCoordinateRegion region_Babelia = { {0.0, 0.0}, {0.0, 0.0}}; 
    region_Babelia.center.latitude = 40.4234778; 
    region_Babelia.center.longitude = -3.686283000000003; 
    region_Babelia.span.longitudeDelta = 0.1f; 
    region_Babelia.span.latitudeDelta = 0.1f; 
    [mapView setRegion:region_Babelia animated:YES]; 

    RestMapPin *ann_Babelia = [[RestMapPin alloc] init]; 
    ann_Babelia.title = @"Babelia"; 
    ann_Babelia.subtitle = @"Barrio de Salamanca"; 
    ann_Babelia.coordinate = region_Babelia.center; 
    [mapView addAnnotation:ann_Babelia]; 

    /*Bacira*/ 
    MKCoordinateRegion region_Bacira = { {0.0, 0.0}, {0.0, 0.0}}; 
    region_Bacira.center.latitude = 40.43375390000001; 
    region_Bacira.center.longitude = -3.699036299999989; 
    region_Bacira.span.longitudeDelta = 0.1f; 
    region_Bacira.span.latitudeDelta = 0.1f; 
    [mapView setRegion:region_Bacira animated:YES]; 

    RestMapPin *ann_Bacira = [[RestMapPin alloc] init]; 
    ann_Bacira.title = @"Bacira"; 
    ann_Bacira.subtitle = @"Chamberí"; 
    ann_Bacira.coordinate = region_Bacira.center; 
    [mapView addAnnotation:ann_Bacira]; 

    /*Bar Galleta*/ 
    MKCoordinateRegion region_bar_galleta = { {0.0, 0.0}, {0.0, 0.0}}; 
    region_bar_galleta.center.latitude = 40.4227336; 
    region_bar_galleta.center.longitude = -3.7036699999999882; 
    region_bar_galleta.span.longitudeDelta = 0.1f; 
    region_bar_galleta.span.latitudeDelta = 0.1f; 
    [mapView setRegion:region_bar_galleta animated:YES]; 

    RestMapPin *ann_bar_galleta = [[RestMapPin alloc] init]; 
    ann_bar_galleta.title = @"Bar Galleta"; 
    ann_bar_galleta.subtitle = @"Malasaña"; 
    ann_bar_galleta.coordinate = region_bar_galleta.center; 
    [mapView addAnnotation:ann_bar_galleta]; 

    /*Bar Tomate*/ 
    MKCoordinateRegion region_bar_tomate = { {0.0, 0.0}, {0.0, 0.0}}; 
    region_bar_tomate.center.latitude = 40.428041; 
    region_bar_tomate.center.longitude = -3.69047; 
    region_bar_tomate.span.longitudeDelta = 0.1f; 
    region_bar_tomate.span.latitudeDelta = 0.1f; 
    [mapView setRegion:region_bar_tomate animated:YES]; 

    RestMapPin *ann_bar_tomate = [[RestMapPin alloc] init]; 
    ann_bar_tomate.title = @"Bar Tomate"; 
    ann_bar_tomate.subtitle = @"Chamberí"; 
    ann_bar_tomate.coordinate = region_bar_tomate.center; 
    [mapView addAnnotation:ann_bar_tomate]; 




    /*Indicador de posicion del mapa (para centrarlo)*/ 
    MKCoordinateRegion region_posicion = { {0.0, 0.0}, {0.0, 0.0}}; 
    region_posicion.center.latitude = 40.44934744420573; 
    region_posicion.center.longitude = -3.695504665374756; 
    region_posicion.span.longitudeDelta = 0.08f; 
    region_posicion.span.latitudeDelta = 0.08f; 
    [mapView setRegion:region_posicion animated:YES]; 
    /*************************************************/ 


} 

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    MKAnnotationView *pinView = nil; 
    if(annotation != mapView.userLocation) { 
     static NSString *defaultPinID = @"com.invasivecode.pin"; 
     pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; 
     if (pinView == nil) 
      pinView = [[MKAnnotationView alloc] 
         initWithAnnotation:annotation reuseIdentifier:defaultPinID]; 


     pinView.canShowCallout = YES; 
     pinView.image = [UIImage imageNamed:@"[email protected]"]; 
    } 
    else { 
     //[mapView.userLocation setTitle:@"I am here"]; 
    } 


    UIButton *pinButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    if ([[annotation title] isEqualToString:@"Bacira"]) { 

    } 
    pinView.rightCalloutAccessoryView = pinButton; 


    return pinView; 
} 

-(IBAction)setMap:(id)sender { 

    switch (((UISegmentedControl *) sender).selectedSegmentIndex) { 
     case 0: 
      mapView.mapType = MKMapTypeStandard; 
      break; 
     case 1: 
      mapView.mapType = MKMapTypeSatellite; 
      break; 
     case 2: 
      mapView.mapType = MKMapTypeHybrid; 
      break; 
     default: 
      break; 
    } 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 

這是我RestMapPin.h:

#import <Foundation/Foundation.h> 
#import <MapKit/MapKit.h> 

@interface RestMapPin : NSObject <MKAnnotation> { 

    CLLocationCoordinate2D coordinate; 
    NSString *title; 
    NSString *subtitle; 
} 

@property (nonatomic, assign) CLLocationCoordinate2D coordinate; 
@property (nonatomic, copy) NSString *title; 
@property (nonatomic, copy) NSString *subtitle; 

@end 

這是我的RestMapPin.m:

#import "RestMapPin.h" 

@implementation RestMapPin 

@synthesize coordinate, title, subtitle; 

@end 

最後,這是該飯店的信息將出現在視圖:RestViewController.h:

#import <UIKit/UIKit.h> 
#import "Restaurant.h" 

@interface RestViewController : UIViewController 

@property (strong, nonatomic) IBOutlet UILabel *TitleLabel; 
@property (strong, nonatomic) IBOutlet UILabel *DescriptionLabel; 
@property (strong, nonatomic) IBOutlet UIImageView *ImageView; 

@property (nonatomic, strong) Restaurant *DetailModal; 

@end 

我konw,我要修改我的viewForAnnotation方法,但我不知道該怎麼做。比你非常感謝你的幫助!

回答

1

用於右細節按鈕

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control { 
    RestMapPin *annView = view.annotation; 

    YourViewController *objYourVC= [[YourViewController alloc]init]; 
    objYourVC.title = annView.title; 
    objYourVC.subtitle = annView. subtitle; 

    [self.navigationController pushViewController:objYourVC animated:YES]; 
} 
+0

對不起,我的目標水平低。我已經寫了這個方法,但Xcode說我「使用未聲明的標識符annView','view'和'annotation'。你知道爲什麼嗎? - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)viewcalloutAccessoryControlTapped :(UIControl *)control {annView = view.annotation; RestViewController * objYourVC = [[RestViewController alloc] init]; objYourVC.TitleLabel = annView。標題; objYourVC.DescriptionLabel = annView。字幕; [self.navigationController pushViewController:objYourVC animated:YES]; }' –

+0

我用RestViewController.h更新了這個問題,即當我按下Map Pin中的按鈕時應該打開的視圖。 –

+0

註釋* annView = view.annotation刪除此和在此線 RestMapPin * annView = view.annotation –

0

此委託方法作爲其它的海報說,使用地圖視圖委託方法mapView:viewCalloutAccessoryControlTapped:

我會反對使用標註的標題來查找餐廳。您不應該使用顯示字符串來索引您的數據。這意味着如果您更改顯示字符串或本地化其他語言,則查找不再有效。

我會爲您的註釋添加一個ID字段並使用它。

+0

我寫了這個方法,但Xcode說我「使用未聲明的標識符'annView','view'和'annotation'你知道爲什麼嗎?我用RestViewController.h更新了這個問題, –

+0

編輯您的代碼以包含產生錯誤的代碼,以及有關哪些特定行會生成錯誤以及所獲得的完整錯誤消息的信息 –

0

你需要讓你的選擇的註釋標題後做這樣的

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control { 
     RestMapPin *selectedAnnotation = view.annotation; 
     NSLog("Title of selected pin - %@", selectedAnnotation.title); 
     // Here you get the title of selected annotation 
} 

現在你需要創建RestViewController的對象,並通過標題在控制器的變量,你一樣是當用戶從列表中選擇任何餐館時做。

另外,正如@Duncan C在他的回答中提到的,您不應該使用String從服務器獲取數據,可以更改字符串。

+0

它可以很好地工作,但是,我有一個問題:'objYourVC.TitleLabel = selectedAnnotation。title'這行'在MapViewController.m中運行良好(例如顯示@「Bacira」),但是當我進入另一個視圖時(稱爲'RestViewController .m','TitleLabel'沒有任何東西(它顯示@「Label」)。有什麼辦法可以解決它,我認爲文本沒有傳遞到另一個視圖。學習,我會用一個整數來獲得dat在下一個項目中)。非常感謝您的幫助! –

+0

調試你的代碼添加斷點和NSLogs – Rajat

相關問題