Bug 38109 - Calling View for a view controller created from a xib causes its constructor to be called twice
Summary: Calling View for a view controller created from a xib causes its constructor ...
Status: RESOLVED ANSWERED
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: XI 9.4 (iOS 9.2)
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Rolf Bjarne Kvinge [MSFT]
URL:
Depends on:
Blocks:
 
Reported: 2016-01-27 20:36 UTC by Dimitar Dobrev
Modified: 2016-01-29 15:33 UTC (History)
4 users (show)

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


Attachments
Code to reproduce the bug (12.92 KB, application/zip)
2016-01-27 20:36 UTC, Dimitar Dobrev
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 ANSWERED

Description Dimitar Dobrev 2016-01-27 20:36:40 UTC
Created attachment 14759 [details]
Code to reproduce the bug

Open the attached project, you'll find the following code:


	public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
	{
		// Override point for customization after application launch.
		// If not required for your application you can safely delete this method

		var p = new PlayerViewController ();
		var v = p.View;
		v.GetHashCode ();

		return true;
	}

...

	public PlayerViewController () : base ("PlayerViewController", null)
	{
		Console.WriteLine ("PlayerViewController () called.");
	}


Run the project and check the log, you'll see this last line twice.
Comment 1 Dimitar Dobrev 2016-01-27 20:37:30 UTC
The first call is correct, the second call is invoked by the line calling View.
Comment 2 Rolf Bjarne Kvinge [MSFT] 2016-01-29 11:51:39 UTC
It seems that when iOS detects a class with the same name as a xib, in some cases it tries to do some magic, which causes a new instance of the class to be created.

The same thing happens in Xcode with a Objective-C sample: https://github.com/rolfbjarne/bug38109, so this is not a bug in Xamarin.iOS.
Comment 3 Dimitar Dobrev 2016-01-29 13:14:37 UTC
I see. Please report it to Apple then. Also, do you think there's a workaround short of not using a xib at all?
Comment 4 Rolf Bjarne Kvinge [MSFT] 2016-01-29 13:26:11 UTC
You can add a dummy argument to the default ctor:

    public PlayerViewController (bool dummy = false)

and it won't be called by iOS.
Comment 5 Dimitar Dobrev 2016-01-29 15:33:17 UTC
It works, thank you.