问题
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);
}
回答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
.
来源:https://stackoverflow.com/questions/9977427/how-to-set-the-background-to-transparent-on-a-dialogviewcontroller