1

When I set the MonoTouch.Dialog background color to uiclear(transparent), it throw an exception, why? and How to set it to transparent.

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object MyDialogViewController.LoadView () [0x00016] in MyDialogViewController.cs: ParentViewController.View.BackgroundColor = UIColor.Clear

public class MyDialogViewController: DialogViewController
{
    public MyDialogViewController (RootElement root)  : base (root) 
    {
    }

    public override void LoadView() 
    {
        base.LoadView ();
        this.TableView.BackgroundColor = UIColor.Clear;
    ParentViewController.View.BackgroundColor = UIColor.Clear;

    }
}


    public void xxxxx(){
      var menu = new RootElement(""){
            new Section ("Demo"){
                new EntryElement("Name", "",""),
            },
        };

        var menuDVC = new MyDialogViewController (menu) {
            Autorotate = true
        };



        this.View.AddSubview(menuDVC.View);
}
poupou
  • 43,413
  • 6
  • 77
  • 174
BlueSky
  • 747
  • 6
  • 19

1 Answers1

1

The NullReferenceException most likely occurs because ParentViewController is null.

Depending on how your MyDialogViewController is showed this might be due to using the wrong property and a recent, iOS5, change:

Prior to iOS 5.0, if a view did not have a parent view controller and was being presented, the presenting view controller would be returned. On iOS 5, this behavior no longer occurs. Instead, use the presentingViewController property to access the presenting view controller.

However if the MyDialogViewController is the window's RootViewController then it's normal for those properties to be null. In this case simply using UIColor.Clear on the TableView get me a black background (I had nothing there) so it should be enough for MT.D part. If you have a parent then you can try to set it's background color to clear (if needed) before displaying your MyDialogViewController.

poupou
  • 43,413
  • 6
  • 77
  • 174
  • thanks your reply. before displaying the MyDiaogViewController(MT.D), I have set the current UIView to UIClear, and also set the mtd.view.backgroudcolor & mtd.tableview.backgroundcolor to uiclear(opaque=false), then add the mtd.view to the current view(this.view.addsubview(mtd.view), but the mtd region is still gray, I want to display with transparent. It's strange. – BlueSky Apr 02 '12 at 16:32
  • 1
    menuDVC(it's MT.D view controller) menuDVC.View.Frame= new RectangleF(20,90,300,550); menuDVC.View.BackgroundColor=UIColor.Clear; menuDVC.View.Opaque=false; menuDVC.TableView.BackgroundColor=UIColor.Clear; menuDVC.TableView.Opaque=false; this.View.AddSubview(menuDVC.View); – BlueSky Apr 02 '12 at 16:54