Registering UITableViewController causes an exception in loadView method

时间:2015-05-24 21:45:23

标签: c# ios uitableview uiviewcontroller xamarin

I'm experimenting with the UITableViewController class in Xamarin and found that when creating a new ViewController in Visual Studio and changing the base-class from UIViewController to UITableViewController, the application crashing upon start with the following exception.

Unhandled Exception that is caused when loading a ViewController

My ViewController basically looked like this:

[Register("MyViewController")]
public class MyViewController: UITableViewController
{
    public override void DidReceiveMemoryWarning()
    {
        base.DidReceiveMemoryWarning();
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
    }
}

Once I removed the [Register("MyViewController")] annotation from the ViewController, it suddenly works. I've tried with and without deleting the xib and/or the designer file but the only way of fixing this exception was removing the annotation.

So my question is, what exactly this Register-annotation is doing, and what is causing an exception when the loadView method is called? Note that no exception is being thrown, if I inherit from UIViewController.

These questions seems to be related, but they are talking about an Interface builder that I am not using, because I'm writing the UI in code with Visual Studio.

1 个答案:

答案 0 :(得分:1)

The Register attribute tells the compiler how to tie a C# class to it's corresponding object in the XIB. Even though you say you are creating your UI in code, it sounds like you are using a template that is creating both a class and a corresponding XIB file. If you are not using XIB, you may just want to use the generic "Empty Class" template instead.