Strange error when loading XIB

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Strange error when loading XIB

KhalilD
Hi,

This problem is driving me crazy, If anyone can help I would very much appreciate it.

I'm trying to load an xib view like so:
NavigationController.PushViewController(new UIViewController("PoscodeInputController",null),true);

The name of the xib is spelled correctly, and the view consists of a few labels and a textbox all with outlets in the view controller.

I keep getting this exception:

Unhandled Exception: MonoTouch.Foundation.MonoTouchException: Objective-C exception thrown.  Name: NSUnknownKeyException Reason: [<UIViewController 0x75993f0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key TextBoxPostCode.
  at (wrapper managed-to-native) MonoTouch.ObjCRuntime.Messaging:void_objc_msgSend_IntPtr_bool (intptr,intptr,intptr,bool)
  at MonoTouch.UIKit.UINavigationController.PushViewController (MonoTouch.UIKit.UIViewController viewController, Boolean animated) [0x0001c] in /Users/plasma/Source/iphone/monotouch/UIKit/UINavigationController.g.cs:121
  at SchoolFinder.SearchRoot.ButtonDoSearchByPostcode (MonoTouch.UIKit.UIButton sender) [0x00000] in /Users/Khalil/Desktop/iOSScratch/SchoolFinder/SchoolFinder-Production/V0.1/SchoolFinder/SearchRoot.xib.cs:39
  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) [0x00038] in /Users/plasma/Source/iphone/monotouch/UIKit/UIApplication.cs:26
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args) [0x00000] in /Users/plasma/Source/iphone/monotouch/UIKit/UIApplication.cs:31
  at SchoolFinder.Application.Main (System.String[] args) [0x00000] in /Users/Khalil/Desktop/iOSScratch/SchoolFinder/SchoolFinder-Production/V0.1/SchoolFinder/Main.cs:14
Reply | Threaded
Open this post in threaded view
|

Re: Strange error when loading XIB

dtavlikos
If we were talking about pure .NET code, this type of error would be similar to an InvalidCastException. Similar, but not identical.

The problem here is that you are initializing the controller with a wrong constructor. You are instructing the runtime to load a native controller of type PoscodeInputController, and use a UIViewController as a wrapper. This cannot stand because when the managed objects will be created, the runtime does not find the members of PoscodeInputController inside the UIViewController class. Hence, the error message actually tells you: "I can't find the member TextBoxPostCode inside UIViewController". You may think why would this happen, since we can do, for example,

UIViewController v = new MyViewController();

where MyViewController inherits UIViewController and might actually have members that UIViewController does not. This is different, since in this case the native object is first created, after that wrapped by the managed MyViewController class and then assigned to an object of its base type.
Here,

> new
> UIViewController("PoscodeInputController",null)

the runtime just fails to wrap the native object to the managed one.

So the best thing is to just initialize your controller with the default constructor:

new PoscodeInputController();

If you check the constructors that MonoDevelop creates, this constructor inherits the constructor you are calling now:

public PoscodeInputController() : base("PoscodeInputController",null) {}

This works because the runtime wraps the native object to its managed wrapper successfully, since it is being called from an object of the same type.

Dimitris Tavlikos
Software Developer
Email: [hidden email]
Twitter: @dtavlikos
Blog: http://software.tavlikos.com





On May 7, 2011, at 10:57 PM, KhalilD wrote:

> Hi,
>
> This problem is driving me crazy, If anyone can help I would very much
> appreciate it.
>
> I'm trying to load an xib view like so:
> NavigationController.PushViewController(new
> UIViewController("PoscodeInputController",null),true);
>
> The name of the xib is spelled correctly, and the view consists of a few
> labels and a textbox all with outlets in the view controller.
>
> I keep getting this exception:
>
> Unhandled Exception: MonoTouch.Foundation.MonoTouchException: Objective-C
> exception thrown.  Name: NSUnknownKeyException Reason: [<UIViewController
> 0x75993f0> setValue:forUndefinedKey:]: this class is not key value
> coding-compliant for the key TextBoxPostCode.
>  at (wrapper managed-to-native)
> MonoTouch.ObjCRuntime.Messaging:void_objc_msgSend_IntPtr_bool
> (intptr,intptr,intptr,bool)
>  at MonoTouch.UIKit.UINavigationController.PushViewController
> (MonoTouch.UIKit.UIViewController viewController, Boolean animated)
> [0x0001c] in
> /Users/plasma/Source/iphone/monotouch/UIKit/UINavigationController.g.cs:121
>  at SchoolFinder.SearchRoot.ButtonDoSearchByPostcode
> (MonoTouch.UIKit.UIButton sender) [0x00000] in
> /Users/Khalil/Desktop/iOSScratch/SchoolFinder/SchoolFinder-Production/V0.1/SchoolFinder/SearchRoot.xib.cs:39
>  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) [0x00038] in
> /Users/plasma/Source/iphone/monotouch/UIKit/UIApplication.cs:26
>  at MonoTouch.UIKit.UIApplication.Main (System.String[] args) [0x00000] in
> /Users/plasma/Source/iphone/monotouch/UIKit/UIApplication.cs:31
>  at SchoolFinder.Application.Main (System.String[] args) [0x00000] in
> /Users/Khalil/Desktop/iOSScratch/SchoolFinder/SchoolFinder-Production/V0.1/SchoolFinder/Main.cs:14
>
>
> --
> View this message in context: http://monotouch.2284126.n4.nabble.com/Strange-error-when-loading-XIB-tp3506100p3506100.html
> Sent from the MonoTouch mailing list archive at Nabble.com.
> _______________________________________________
> MonoTouch mailing list
> [hidden email]
> http://lists.ximian.com/mailman/listinfo/monotouch

_______________________________________________
MonoTouch mailing list
[hidden email]
http://lists.ximian.com/mailman/listinfo/monotouch
Reply | Threaded
Open this post in threaded view
|

Re: Strange error when loading XIB

KhalilD
Cool, thanks alot for the excellent explanation, Guess I should really pay more attention to the generated code :). Anyway instantiating it as the derived type worked so thank you once again.