Bug 7532 - MKMapView events not hooked up
Summary: MKMapView events not hooked up
Status: RESOLVED INVALID
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: 6.0.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-09-28 11:24 UTC by Yvan Rodrigues
Modified: 2012-09-30 21:06 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Demo project (5.65 MB, application/x-zip-compressed)
2012-09-28 11:24 UTC, Yvan Rodrigues
Details
Demo project using MKMapView loaded from XIB (5.92 KB, application/octet-stream)
2012-09-30 11:43 UTC, Yvan Rodrigues
Details
three scenarios (5.66 MB, application/octet-stream)
2012-09-30 20:44 UTC, Yvan Rodrigues
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED INVALID

Description Yvan Rodrigues 2012-09-28 11:24:26 UTC
Created attachment 2645 [details]
Demo project

As far as I can tell, MKMapView's events, such as DidAddAnnotationViews and MapLoaded aren't hooked up.

To reproduce:
1. Create a class that inherits UIViewController.
2. Create am MKMapView
3. Attach a delegate to the above events.
4. Add the view to the VC.
Result: map displays but delegates are never called.

The following demonstrates this behaviour. 

using MonoTouch.Foundation;
using MonoTouch.MapKit;
using MonoTouch.UIKit;

namespace MapEventsBroken
{
	[Register ("AppDelegate")]
	public partial class AppDelegate : UIApplicationDelegate
	{
		UIWindow window;

		public override bool FinishedLaunching(UIApplication app, NSDictionary options)
		{
			window = new UIWindow (UIScreen.MainScreen.Bounds);
			
			var map = new MKMapView(window.Frame);
			map.MapLoaded += (sender, e) => new UIAlertView("Alert!", "You should see me when the map loads.", null, "I do!", null);
			window.AddSubview(map);
			
			window.MakeKeyAndVisible ();			
			return true;
		}
	}
}
Comment 1 Sebastien Pouliot 2012-09-28 14:17:17 UTC
You're creating an UIAlertView instance but you're not doing anything with it.

Change:

new UIAlertView("Alert!", "You should see me when the map loads.", null, "I do!", null)

to

new UIAlertView("Alert!", "You should see me when the map loads.", null, "I do!", null).Show ()

and it will be shown.
Comment 2 Yvan Rodrigues 2012-09-28 14:25:20 UTC
1. You're 100% right about me forgetting Show() in my test case. (duh!)

2. Nope -- not shown, with the correction.
Comment 3 Sebastien Pouliot 2012-09-28 14:31:26 UTC
Works here, code I used:

using MonoTouch.Foundation;
using MonoTouch.MapKit;
using MonoTouch.UIKit;

namespace MapEventsBroken
{
	[Register ("AppDelegate")]
	public partial class AppDelegate : UIApplicationDelegate
	{
		UIWindow window;

		public override bool FinishedLaunching(UIApplication app, NSDictionary options)
		{
			window = new UIWindow (UIScreen.MainScreen.Bounds);

			var map = new MKMapView(window.Frame);
			map.MapLoaded += (sender, e) => { 
				new UIAlertView("Alert!", "You should see me when the map loads.", null, "I do!", null).Show ();
			};
			window.AddSubview(map);

			window.MakeKeyAndVisible ();			
			return true;
		}
	}
}


Note that (with iOS6) you'll run into some weird issue if you don't set the `window.RootViewController` to a view controller (you should have a warning printed out in your application output pad).
Comment 4 Yvan Rodrigues 2012-09-30 11:39:02 UTC
1. When I ran this project on a real device it worked -- it appears to work intermittently on the simulator.

2. I think when I originally encountered this behaviour I was loading the MKMapView as part of a UIViewController that loaded a XIB in the constructor.

If you see the attached project, the class subscribes to the map's events in its override of ViewDidLoad (I don't think this can be dnoe any earlier due to the view's lifecycle).

In this case the the delegate subscribed to MapLoaded is not fired, perhaps because the map was loaded prior to subscription of the event?

However, DidAddAnnotationViews is also subscribed at the same time, but does not fire later when an annotation is added.

Any thoughts?

PS: Thanks for the info re source code, but none of it included MonoTouch.UIKit -- these must be proprietary?
Comment 5 Yvan Rodrigues 2012-09-30 11:43:26 UTC
Created attachment 2651 [details]
Demo project using MKMapView loaded from XIB
Comment 6 Yvan Rodrigues 2012-09-30 13:49:11 UTC
There's *something* here -- I just spent another hour fighting this on another project, but I can't put together the contributing factors.

I'll close this and maybe if someone else encounters this they can add more info.
Comment 7 Yvan Rodrigues 2012-09-30 18:36:56 UTC
Reopening this. I've been fighting this for 6 hours. Ran the project in the simulator again -- no events fired.

I don't think this is (necessarily) about events. There's some sort of weird timing/state thing going on. In my real-world project if I set MKMapView.Delegate after instantiating the view it never gets invoked, but if I move it one line down it does get invoked. The line in between simply adds an event handler.

More info as I get it...
Comment 8 Sebastien Pouliot 2012-09-30 19:08:44 UTC
MKMapView.Delegate ? you can't mix XDelegate with X's events since they are the same thing, exposed differently (in the ObjC way and in the .NET way).

Reference: http://spouliot.wordpress.com/2012/03/26/events-vs-objective-c-delegates/
Comment 9 Yvan Rodrigues 2012-09-30 19:52:48 UTC
This case is a little different.

MKMapView.Delegate is a property of type MKMapViewDelegate. You set it to a callback that is invoked each time an annotation is shown on the map. The delegate is responsible for creating the annotation.

There isn't a corresponding event (MulticastDelegate).
Comment 10 Yvan Rodrigues 2012-09-30 20:44:53 UTC
Created attachment 2658 [details]
three scenarios

Sebastien, what sort of results do you get with the attached project?
Comment 11 Yvan Rodrigues 2012-09-30 21:06:21 UTC
I see what you're talking about Sebastien -- you're saying that within the MKMapViewDelegate class there are individual delegates, some of which correspond to the events that MonoTouch has implemented in the MKMapView class.
Therefore I need to use all of one or all of the other.. 10-4. thanks.