17

I just upgraded my Monotouch to 6 and now my app won't start. It was working formerly without any issues. Now it throws an exception (listed below) in the Main.cs file. I've looked through the troubleshooting tips on Xamarin, but it didn't resolve the issue. I've re-layed out the nib file, removed and re-configured my outlets, and have create an entirely new nib to see if that would remedy the problem. Does anybody else have any thoughts?

MonoTouch.Foundation.MonoTouchException: Objective-C exception thrown.  Name: NSUnknownKeyException Reason: [<UIApplication 0xc84bb10> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key btnNewAccount.
   at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
   at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
   at Pokr.Application.Main (System.String[] args) [0x00000] in /Users/James/Projects/App/Main.cs:17

Code from the LoginView.designer.cs:

[Register ("LoginView")]
partial class LoginView
{
    [Outlet]
    MonoTouch.UIKit.UIImageView imgLogo { get; set; }

    [Outlet]
    MonoTouch.UIKit.UITextField txtEmail { get; set; }

    [Outlet]
    MonoTouch.UIKit.UITextField txtPassword { get; set; }

    [Outlet]
    MonoTouch.UIKit.UIButton btnLogin { get; set; }

    [Outlet]
    MonoTouch.UIKit.UIButton btnNewAccount { get; set; }

    [Outlet]
    MonoTouch.UIKit.UILabel lblSecurityNotice { get; set; }

    [Outlet]
    MonoTouch.UIKit.UIImageView imgKeyboardBorder { get; set; }

    void ReleaseDesignerOutlets ()
    {
        if (imgLogo != null) {
            imgLogo.Dispose ();
            imgLogo = null;
        }

        if (txtEmail != null) {
            txtEmail.Dispose ();
            txtEmail = null;
        }

        if (txtPassword != null) {
            txtPassword.Dispose ();
            txtPassword = null;
        }

        if (btnLogin != null) {
            btnLogin.Dispose ();
            btnLogin = null;
        }

        if (btnNewAccount != null) {
            btnNewAccount.Dispose ();
            btnNewAccount = null;
        }

        if (lblSecurityNotice != null) {
            lblSecurityNotice.Dispose ();
            lblSecurityNotice = null;
        }

        if (imgKeyboardBorder != null) {
            imgKeyboardBorder.Dispose ();
            imgKeyboardBorder = null;
        }
    }

Code from Main.cs (where the code breaks):

    static void Main (string[] args)
    {
        UIApplication.Main (args, null, "AppDelegate");
    }

Here is the snippet from my AppDelegate where I call the ViewController:

        var rootNavigationController = new UINavigationController();

        LoginView loginScreen = new LoginView();
        rootNavigationController.PushViewController(loginScreen, false);

        this.window.RootViewController = rootNavigationController;

        //blank function fires so the getter will init the singleton.
        Singleton.Instance.Initialize();

        // make the window visible
        window.MakeKeyAndVisible ();


        return true;
jamesbar2
  • 614
  • 1
  • 9
  • 20
  • Added some code for, it's just the designer generated code. But, it should give you an idea of what the designer is syncing. As I said, it worked before upgrading. So, I'm curious if it's a Monotouch/develop bug. – jamesbar2 Sep 24 '12 at 23:52

5 Answers5

18

This error occurs when you have initialized a ViewController in code but also have the ViewController initialized from a XIB file.

This could happen if you have the "Main Interface" value set to a ViewController you're creating in code. To resolve this issue, make this value empty, then no ViewController will be initialized automatically.

Also check your pInfo file to see if there is a Main Interface set.

Thanks to @Bart for providing this tip for Xamarin users:

Right-click project in Xamarin Studio (v.4) select 'Options' and then under 'iOS Project' (section 'iPad Deployment info'). Clear out the drop-down 'Main Interface' and that should solve the issue.

gotnull
  • 26,454
  • 22
  • 137
  • 203
  • 2
    This fixed it! Thanks a bunch. It was in my application settings that had defined a main interface. – jamesbar2 Sep 25 '12 at 18:12
  • 2
    Thanks! Same here: right-click project in Xamarin Studio (v.4) select 'Options' and then under 'iOS Project' (section 'iPad Deployment info') I had to clear out the dropdown 'Main Interface' and that solved it. – Bart Oct 24 '13 at 10:29
3

It's telling you the reason: [<UIApplication 0xc84bb10> setValue:forUndefinedKey:]. Notice it's not saying: [<LoginView 0xc84bb10> setValue:forUndefinedKey:]. Now you need to figure out what is trying to send the message -setBtnNewAccount: to an instance of UIApplication.

It looks like a delegate is set wrong somewhere.

Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
  • Hmm. I'm not sure how the delegate is set wrong since its assigned by the designer/mono develop. I do notice that though, and am confused why its in the root of the app and not in the controller. Maybe something has changed in how iOS 6 calls functions? – jamesbar2 Sep 25 '12 at 02:07
2

I had similar issues with creating outlets from buttons, thus removing those and creating outlets from them. Somewhere the sync between XCode and Xamarin were not completely done in a correct way...

Fix for me was to remove the buttons, followed by searching and removing all references in the designer files manually.

Hope this helps...

Hutjepower
  • 1,241
  • 12
  • 17
1

I faced this issue even with in a recent build of Xamarin Studio (5.9.5). The "Sync with Xcode" option was greyed out but evidently my ViewController xib file in Tamarin was not in sync with Xcode.

The fix was the same as mentioned by Hutjepower. I searched for the broken reference within Xamarin Studio, opened up the xib file within Xamarin (right click on the file and use a Source Code Editor) and then removed the controls that I had removed through Xcode. That fixed this issue.

BBC
  • 396
  • 4
  • 12
1

I've been having this issue with Xamarin Studio 5.10.1 as well; I eventually discovered that if I used a text editor to edit my storyboard file and removed everything in the

<connections></connections>

it fixed the problem for me.

VLC
  • 9
  • 1