0

I am new in xamarin ios, my app is crashed when running the app and throws an exception "Objective-C exception thrown. Name: NSInternalInconsistencyException Reason: Could not load NIB in …".How can I manage it..

Please help me...

Here is my code ViewController

 public partial class LoginViewController : UIViewController
{
    public LoginViewController(IntPtr handle) : base(handle)
    {
    }

    public LoginViewController() :base("LoginViewController",null)
    {

    }

    public override void DidReceiveMemoryWarning()
    {
        // Releases the view if it doesn't have a superview.
        base.DidReceiveMemoryWarning();

        // Release any cached data, images, etc that aren't in use.
    }

    #region View lifecycle

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

    public override void ViewWillAppear(bool animated)
    {
        base.ViewWillAppear(animated);
    }

    public override void ViewDidAppear(bool animated)
    {
        base.ViewDidAppear(animated);
    }

    public override void ViewWillDisappear(bool animated)
    {
        base.ViewWillDisappear(animated);
    }

    public override void ViewDidDisappear(bool animated)
    {
        base.ViewDidDisappear(animated);
    }

    partial void Btn_Login_TouchUpInside(UIButton sender)
    {


    }

    #endregion
}

here is my AppDelegate

   public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
    {
        UIWindow window;
        UINavigationController navigationController;
        UIViewController viewController;

        window = new UIWindow(UIScreen.MainScreen.Bounds);

        viewController = new LoginViewController();

        navigationController = new UINavigationController();
        navigationController.PushViewController(viewController, false);

        window.RootViewController = navigationController;

        window.MakeKeyAndVisible();

        return true;
    }

I got an exception in Main.cs

 public class Application
{
    // This is the main entry point of the application.
    static void Main(string[] args)
    {
        // if you want to use a different Application Delegate class from "AppDelegate"
        // you can specify it here.
        try
        {
            UIApplication.Main(args, null, "AppDelegate");

        }
        catch(System.Exception ex)
        {

        }
    }
}
Swathy
  • 199
  • 1
  • 5
  • 24

1 Answers1

0

Objective-C exception thrown. Name: NSInternalInconsistencyException Reason: Could not load NIB in …

If you initalize viewController using viewController = new LoginViewController(); , and the class have the constructors public LoginViewController() :base("LoginViewController",null) , it will look for .xib file when running, but Visual Studio removed the template of viewController with xib since 15.7, you must create viewcontroller with storyboard, so the error occurs.

You can follow this tutorial to implement the way like before.


But we strongly recommend you to create viewcontroller as below.

Workaround 1

Create a new class inherit from UIViewController(just a single .cs)

[Register("LoginViewController")]
public class LoginViewController : UIViewController
{
    public LoginViewController()
    {
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        this.View.BackgroundColor = UIColor.Red;
        // Perform any additional setup after loading the view
    }
}

But in this way you have to manage UI programmatically.

Workaround 2

Create viewController with Storyboard, you can drag ViewController from Toolbox to the designer , specify the Class and provide Storyboard ID.

enter image description here

In AppDelegate modify the way that initalizing the viewcontroller.

//viewController = new LoginViewController();
viewController = UIStoryboard.FromName("LoginViewController",null).InstantiateViewController("LoginViewController");

A little thing

Window is global variable in AppDelegate and used in the whole application, you should never create a local variable UIWindow ,just use the default Window.

ColeX
  • 14,062
  • 5
  • 43
  • 240