1
votes

I'm having a problem with mapping controls in a subview back to fields on the owning controller. Specifically, I have mapped outlets for each of my "controls" to File's Owner. Monotouch then generated code for the controller's xib designer.cs file to reference these controls as properties on the controller class. However, when I run my code; I get object is null errors when trying to set properties on the controls. Digging into the issue with the debugger; it appears GetNativeField is returning null when trying to access the outlets by their names from the xib file.

Anyone have any ideas why this would be happening? I've checked the .xib file, and the generated code; the Outlet, Property, and Field names are consistent with one another.

3

3 Answers

0
votes

Note that the outlets will only be accessible after the view has been loaded and that happens in LoadView() or by accessing the UIViewController's "View" property (this will load the view implicitly). Only then the the IB content gets constructed and is available for use.

So if you want to change stuff, you would either manually call LoadView(), or override it in your view, call base.LoadView() and then access the outlets.

0
votes

I figured out what was causing the issue; it was how I was pushing the controller on the stack:

using(var batteryController = new BatteryController()){

    navigationController.PushViewController(batteryController,true);

}

It seems that when dispose is called on the controller, the NIB resource is removed from memory; which was causing the issue.

However, this brings up another question. Aren't you supposed to call dispose on the new controller once it's been pushed onto the stack? In objective C, when you push a controller on the stack; your supposed to call release afterwards. So what am I doing wrong then?

-1
votes

you should add the outlets on the AppDelegate. give this a try also on my blog it's a video of a simple calculator, it's in spanish but if you watch it its very self explanatory you can watch it here http://alexsoto.me/calculadora-monotouch it should help you to get started :) hope this helps Good luck, if i can help you on anything else just let me know

Edit: also you can check this video this one uses subviews also should help you, this one its in english http://www.alexyork.net/blog/post/Selecting-a-contact-from-the-Address-Book-with-MonoTouch.aspx