2012-10-03 71 views
1

我有一個問題,當用戶單擊標註時,CalloutAccessoryControlTapped永遠不會被調用。這是否已在Apple地圖中刪除或更改?Apple地圖CalloutAccessoryControlTapped永不開火

在iOS 5中,我的電話出這個樣子:

(圖片)餐廳名稱/地址>

當我點擊通話的時候,我會得到一個對話框。在iOS6的,我的電話了是這樣的:

RestaurantName /地址

爲什麼我召喚出蘋果地圖改變了嗎?

下面是它看起來像在iOS 6中:

Has Apple Maps removed the CalloutAccessControlTapped feature?

這裏是我的代碼:

using System; 
using System.Drawing; 
using MonoTouch.Foundation; 
using MonoTouch.UIKit; 
using MonoTouch.MapKit; 
using MonoTouch.CoreLocation; 
using System.Linq; 
using MenuFinderAN.BusinessLogic.MenuFinderServiceReference; 
using System.Collections.Generic; 

namespace MenuFinderMT 
{ 
public partial class MapUniversalController : UIViewController 
{ 
    static bool UserInterfaceIdiomIsPhone { 
     get { return UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone; } 
    } 

    public AppDelegate AppDelegate { get; set; } 

    public List<IGrouping<string, RestaurantLocation>> LocationList { get; set; } 

    public MapUniversalController (List<IGrouping<string, RestaurantLocation>> locationList) 
     : base (UserInterfaceIdiomIsPhone ? "MapUniversalController_iPhone" : "MapUniversalController_iPad", null) 
    { 
     LocationList = locationList; 
    } 

    public override void DidReceiveMemoryWarning() 
    { 
     // Releases the view if it doesn't have a superview. 
     base.DidReceiveMemoryWarning(); 

     // Release any cached data, images, etc that aren't in use. 
    } 

    MKMapView mapView; 

    public override void ViewDidLoad() 
    { 
     base.ViewDidLoad(); 
     AppDelegate = UIApplication.SharedApplication.Delegate as AppDelegate; 
     Title = "Locations"; 

     mapView = new MKMapView (View.Bounds); 
     mapView.AutoresizingMask = UIViewAutoresizing.FlexibleDimensions;  
     //mapView.MapType = MKMapType.Standard; // this is the default 
     //mapView.MapType = MKMapType.Satellite; 
     //mapView.MapType = MKMapType.Hybrid; 
     View.AddSubview (mapView); 

     List<RestaurantLocation> locations = LocationList.SelectMany (x => x).ToList(); 

     // create our location and zoom 

     double lat = 0; 
     double lng = 0; 

     foreach (RestaurantLocation loc in locations) { 
      if (loc.Latitude > 0 && lat == 0) { 
       lat = loc.Latitude; 
       lng = loc.Longitude; 
      } 

      // add a basic annotation 
      var annotation = new BasicMapAnnotation (new CLLocationCoordinate2D (loc.Latitude, loc.Longitude), loc.Name, loc.Address + ", " + loc.City); 
      mapView.AddAnnotation (annotation); 
     } 

     CLLocationCoordinate2D coords = new CLLocationCoordinate2D (lat, lng); 

     MKCoordinateSpan span = new MKCoordinateSpan (MilesToLatitudeDegrees (20), MilesToLongitudeDegrees (20, coords.Latitude)); 

     // set the coords and zoom on the map 
     mapView.Region = new MKCoordinateRegion (coords, span); 


     // assign the delegate, which handles annotation layout and clicking 
     mapView.Delegate = new MapDelegate (this, locations); 


    } 

    /// <summary> 
    /// Converts miles to latitude degrees 
    /// </summary> 
    public double MilesToLatitudeDegrees (double miles) 
    { 
     double earthRadius = 3960.0; 
     double radiansToDegrees = 180.0/Math.PI; 
     return (miles/earthRadius) * radiansToDegrees; 
    } 

    /// <summary> 
    /// Converts miles to longitudinal degrees at a specified latitude 
    /// </summary> 
    public double MilesToLongitudeDegrees (double miles, double atLatitude) 
    { 
     double earthRadius = 3960.0; 
     double degreesToRadians = Math.PI/180.0; 
     double radiansToDegrees = 180.0/Math.PI; 

     // derive the earth's radius at that point in latitude 
     double radiusAtLatitude = earthRadius * Math.Cos (atLatitude * degreesToRadians); 
     return (miles/radiusAtLatitude) * radiansToDegrees; 
    } 

    public override void ViewDidUnload() 
    { 
     base.ViewDidUnload(); 

     // Clear any references to subviews of the main view in order to 
     // allow the Garbage Collector to collect them sooner. 
     // 
     // e.g. myOutlet.Dispose(); myOutlet = null; 

     ReleaseDesignerOutlets(); 
    } 
} 

// The map delegate is much like the table delegate. 
class MapDelegate : MKMapViewDelegate 
{ 
    protected string annotationIdentifier = "BasicAnnotation"; 
    MapUniversalController mapUniversalController; 
    List<RestaurantLocation> locations; 

    public MapDelegate (MapUniversalController mapUniversalController, List<RestaurantLocation> locations) 
    { 
     this.mapUniversalController = mapUniversalController; 
     this.locations = locations; 
    } 

    /// <summary> 
    /// This is very much like the GetCell method on the table delegate 
    /// </summary> 
    public override MKAnnotationView GetViewForAnnotation (MKMapView mapView, NSObject annotation) 
    { 
     // try and dequeue the annotation view 
     MKAnnotationView annotationView = mapView.DequeueReusableAnnotation (annotationIdentifier); 

     // if we couldn't dequeue one, create a new one 
     if (annotationView == null) 
      annotationView = new MKPinAnnotationView (annotation, annotationIdentifier); 
     else // if we did dequeue one for reuse, assign the annotation to it 
      annotationView.Annotation = annotation; 

     // configure our annotation view properties 
     annotationView.CanShowCallout = true; 
     (annotationView as MKPinAnnotationView).AnimatesDrop = true; 
     (annotationView as MKPinAnnotationView).PinColor = MKPinAnnotationColor.Green; 
     annotationView.Selected = true; 

     // you can add an accessory view, in this case, we'll add a button on the right, and an image on the left 
     annotationView.RightCalloutAccessoryView = UIButton.FromType (UIButtonType.DetailDisclosure); 

     annotationView.LeftCalloutAccessoryView = new UIImageView (UIImage.FromBundle ("Images/Icons/MenuFinder_Logo-29x29.png")); 

     return annotationView; 
    } 

    public override void CalloutAccessoryControlTapped (MKMapView mapView, MKAnnotationView view, UIControl control) 
    { 
     var annotation = view.Annotation; 
     var coords = (annotation as MKAnnotation).Coordinate; 
     RestaurantLocation restaurantLocation = locations.Where (a => a.Latitude == coords.Latitude && a.Longitude == coords.Longitude).FirstOrDefault(); 
     UIAlertView alertt = new UIAlertView ("Directions?", "Would you like directions?", null, "Yes", null); 
     alertt.AddButton ("No"); 
     alertt.Clicked += delegate(object sender, UIButtonEventArgs e) { 
      if (e.ButtonIndex == 0) { 
       double slat = (double)mapUniversalController.AppDelegate.Latitude; 
       double slng = (double)mapUniversalController.AppDelegate.Longitude; 
       string saddr = slat.ToString() + "," + slng.ToString(); 
       string daddr = coords.Latitude.ToString() + "," + coords.Longitude.ToString(); 
       NSUrl url = new NSUrl ("http://maps.google.com/maps?saddr=" + saddr + "&daddr=" + daddr); 
       UIApplication.SharedApplication.OpenUrl (url); 
      } else { 
       mapUniversalController.AppDelegate.CurrentRestaurantLocation = restaurantLocation; 
       // Pass the selected object to the new view controller. 
       mapUniversalController.NavigationController.PushViewController (new RestaurantDetailsUniversalController(), true); 
      } 
     }; 
     alertt.Show(); 


    } 

    // as an optimization, you should override this method to add or remove annotations as the 
    // map zooms in or out. 
    public override void RegionChanged (MKMapView mapView, bool animated) 
    { 
    } 
} 

class BasicMapAnnotation : MKAnnotation 
{ 
    /// <summary> 
    /// The location of the annotation 
    /// </summary> 
    public override CLLocationCoordinate2D Coordinate { get; set; } 

    protected string title; 
    protected string subtitle; 

    /// <summary> 
    /// The title text 
    /// </summary> 
    public override string Title 
    { get { return title; } } 


    /// <summary> 
    /// The subtitle text 
    /// </summary> 
    public override string Subtitle 
    { get { return subtitle; } } 

    public BasicMapAnnotation (CLLocationCoordinate2D coordinate, string title, string subTitle) 
     : base() 
    { 
     this.Coordinate = coordinate; 
     this.title = title; 
     this.subtitle = subTitle; 
    } 
} 

}

回答

5

委託處理行爲已與iOS 6的微變確保在添加任何註釋之前分配MapDelegate。

編輯:見rule #2設置屬性或使用實例(博客有更多的細節,爲什麼這很重要,不只是在MonoTouch中)

之前設置委託或所有事件