不符合键值编码规范(Monotouch和iOS 6)

17

我刚刚将Monotouch升级到6,现在我的应用程序无法启动。之前它一直正常工作。现在它在Main.cs文件中抛出异常(如下所示)。我已经查看了Xamarin上的故障排除提示,但未解决问题。我已经重新布置了nib文件,移除并重新配置了我的outlets,并创建了一个全新的nib文件,看看是否可以解决问题。还有其他人有什么想法吗?

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

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;
        }
    }

从 Main.cs 中的代码(导致代码出现问题的地方):

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

以下是我在AppDelegate中调用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;

添加了一些代码,这只是设计师生成的代码。但是,它应该让你了解设计师正在同步的内容。正如我所说,升级之前它是可以工作的。因此,我很好奇这是否是Monotouch/develop的错误。 - jamesbar2
5个回答

18

当你在代码中初始化了一个ViewController,同时也从XIB文件初始化了ViewController时,就会发生此错误。

如果你将"Main Interface"的值设置为在代码中创建的ViewController,则可能会发生这种情况。要解决此问题,请将此值为空,然后不会自动初始化任何ViewController

还要检查您的pInfo文件,看是否设置了主接口(Main Interface)。

感谢@BartXamarin用户提供此提示:

在Xamarin Studio(v.4)中右键单击项目,选择“选项”,然后在“iOS项目”下(部分“iPad部署信息”)清除下拉菜单中的“Main Interface”,这样应该就可以解决问题了。


2
搞定了!非常感谢。问题出在我的应用程序设置中,定义了一个主接口。 - jamesbar2
2
谢谢!同样的问题:在Xamarin Studio(版本4)中,右键单击项目,选择“选项”,然后在“iOS项目”(“iPad部署信息”部分)下,我需要清除下拉菜单中的“主界面”,这样问题就解决了。 - Bart

3

它告诉你原因:[<UIApplication 0xc84bb10> setValue:forUndefinedKey:]。注意它没有说:[<LoginView 0xc84bb10> setValue:forUndefinedKey:]。现在你需要弄清楚是谁试图向一个UIApplication实例发送消息-setBtnNewAccount:

看起来某个地方设置了错误的代理。


嗯,我不确定委托是如何设置错误的,因为它是由设计师/mono develop分配的。我确实注意到了这一点,但是对于为什么它在应用程序的根目录而不是控制器中感到困惑。也许iOS 6在调用函数方面有所改变? - jamesbar2

2

我曾经遇到过从按钮创建出口的类似问题,因此删除这些按钮并从它们创建出口。在某个地方,XCode与Xamarin之间的同步没有完全正确执行...

对我来说,解决方法是删除按钮,然后手动搜索并删除设计文件中的所有引用。

希望这可以帮助到您...


1
我也遇到了Xamarin Studio 5.10.1的这个问题;最终我发现,如果我使用文本编辑器编辑我的storyboard文件并删除其中所有内容,然后重新打开它,问题就得到了解决。
<connections></connections>

它为我解决了这个问题。

1
我甚至在最近的Xamarin Studio版本(5.9.5)中也遇到了这个问题。 "与Xcode同步"选项被禁用,但是我的Tamarin中的ViewController xib文件显然与Xcode不同步。
解决方法与Hutjepower提到的相同。我在Xamarin Studio中搜索了损坏的引用,在Xamarin中打开了xib文件(右键单击该文件并使用源代码编辑器),然后删除了我通过Xcode删除的控件。这解决了此问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接