Bug 1620 - Cannot inherit from UIAlertView
Summary: Cannot inherit from UIAlertView
Status: RESOLVED NOT_ON_ROADMAP
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: 5.0
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2011-10-20 23:00 UTC by Andrew Young
Modified: 2012-02-07 08:13 UTC (History)
3 users (show)

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


Attachments
sample project (8.33 KB, application/zip)
2011-10-20 23:01 UTC, Andrew Young
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 NOT_ON_ROADMAP

Description Andrew Young 2011-10-20 23:00:00 UTC
http://stackoverflow.com/questions/7692522/subclassed-uialertview-not-shown


App crashes when I try to inherit from UIAlertView. See stack trace and sample code below. MonoTouch 5.0.

Any ideas? I have a sample app that shows this for anyone interested.

public class ViewController : UIViewController
{
  private DateTimePickerView _pickerView;
  public ViewController ()
  {
  }

  public override void ViewDidLoad ()
  {
    base.ViewDidLoad ();
    new NotReachableAlertView ().Show ();
  }
}

public class NotReachableAlertView : UIAlertView
{
  public NotReachableAlertView() : base("Title", "Message", null, "Cancel")
  {
  }
}

Unhandled Exception: MonoTouch.Foundation.MonoTouchException: Objective-C exception thrown.  Name: NSInternalInconsistencyException Reason: Requesting the window of a view (<PickerViewCrash.NotReachableAlertView: 0x79a700; baseClass = UIAlertView; frame = (0 0; 0 0); transform = [0, 0, 0, 0, 0, 0]; alpha = 0; opaque = NO; layer = (null)>) with a nil layer. This view probably hasn't received initWithFrame: or initWithCoder:.
  at MonoTouch.UIKit.UIAlertView.Show () [0x00000] in <filename unknown>:0 
  at PickerViewCrash.ViewController.ViewDidLoad () [0x00006] in /Users/Andrew/Projects/PickerViewCrash/ViewController.cs:20 
  at MonoTouch.UIKit.UIWindow.MakeKeyAndVisible () [0x00000] in <filename unknown>:0 
  at PickerViewCrash.AppDelegate.FinishedLaunching (MonoTouch.UIKit.UIApplication app, MonoTouch.Foundation.NSDictionary options) [0x00031] in /Users/Andrew/Projects/PickerViewCrash/AppDelegate.cs:32 
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00000] in <filename unknown>:0 
  at PickerViewCrash.Application.Main (System.String[] args) [0x00000] in /Users/Andrew/Projects/PickerViewCrash/Main.cs:16 
[ERROR] FATAL UNHANDLED EXCEPTION: MonoTouch.Foundation.MonoTouchException: Objective-C exception thrown.  Name: NSInternalInconsistencyException Reason: Requesting the window of a view (<PickerViewCrash.NotReachableAlertView: 0x79a700; baseClass = UIAlertView; frame = (0 0; 0 0); transform = [0, 0, 0, 0, 0, 0]; alpha = 0; opaque = NO; layer = (null)>) with a nil layer. This view probably hasn't received initWithFrame: or initWithCoder:.
  at MonoTouch.UIKit.UIAlertView.Show () [0x00000] in <filename unknown>:0 
  at PickerViewCrash.ViewController.ViewDidLoad () [0x00006] in /Users/Andrew/Projects/PickerViewCrash/ViewController.cs:20 
  at MonoTouch.UIKit.UIWindow.MakeKeyAndVisible () [0x00000] in <filename unknown>:0 
  at PickerViewCrash.AppDelegate.FinishedLaunching (MonoTouch.UIKit.UIApplication app, MonoTouch.Foundation.NSDictionary options) [0x00031] in /Users/Andrew/Projects/PickerViewCrash/AppDelegate.cs:32 
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00000] in <filename unknown>:0 
  at PickerViewCrash.Application.Main (System.String[] args) [0x00000] in /Users/Andrew/Projects/PickerViewCrash/Main.cs:16 
Terminating runtime due to unhandled exception
Comment 1 Andrew Young 2011-10-20 23:01:36 UTC
Created attachment 740 [details]
sample project

here's a sample of the crash with the UIAlertView.
Comment 2 Jeffrey Stedfast 2011-11-10 14:42:23 UTC
This seems like the culprit:

"This view probably hasn't received initWithFrame: or initWithCoder:."

I guess if you subclass, you need to use one of those 2 ctors rather than initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:

Maybe there is a fix we can do in MonoTouch, though... not sure.
Comment 3 Rolf Bjarne Kvinge [MSFT] 2012-02-06 19:18:57 UTC
According to Apple, UIAlertView must not be subclassed: "The UIAlertView class is intended to be used as-is and does not support subclassing." (https://developer.apple.com/library/ios/#documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html)

We tried to enforce this in MonoTouch, but it broke too much code for people so we had to revert it (but that doesn't mean it's supported).
Comment 4 Jeffrey Stedfast 2012-02-07 08:13:10 UTC
FWIW, you *can* subclass UIAlertViewDelegate and pass that into the UIAlertView .ctor - which is what I am doing in my own app.

Perhaps that would give you all of the control you need for your app.