On 24 November 2011 03:56, Karl Heinz Brehme Arredondo
<[email protected]> wrote:
> I don't know if it can be a bug on MonoTouch, but I made a workaround with
> InvokeMember, getting the control by name with reflection…

That's the correct solution.

For the full explanation:

When you load a nib, UIKit deserializes the object tree and assigns
object references to the connected fields/properties on the File's
Owner.

In older versions of MonoTouch, the [Connect] attribute was used. This
added an ivar (basically a native Obj-C field) to your class, and
UIKit would assign the deserialized object to that field. The
MonoTouch-generated property would use GetNativeField to get the
native field by name, and keep a reference to the managed wrapper on a
managed field.

The code generated by MonoDevelop 2.8 uses the new [Outlet] attribute.
This makes the managed property directly visible to obj-c, so obj-c
can assign the deserialized object directly to the managed property
(and therefore its backing field) without any need for a native field.
This simplifies the generated code and eliminates the ivar.

For what you're trying to do, reflecting over managed properties is
the "more modern" equivalent of Get/SetNativeField.

-- Michael

> From: Karl Heinz Brehme Arredondo <[email protected]>
> Date: Wed, 23 Nov 2011 22:58:31 -0200
> To: "[email protected]" <[email protected]>
> Subject: [MonoTouch] GetNativeField don't get some controls anymore
>
> Hi,
> I have a method that use GetNativeField to get the instance of a control to
> make dinamic manipulaton of text and other things from database, like
> localization. So each view that is open it changes things to the user.
> But, some ViewControllers was converted to the new "template" of MonoTouch
> 2.8 x Xcode 4 like:
>
> [Outlet]
>
> MonoTouch.UIKit.UINavigationBar lblTitle { get; set; }
>
> [Outlet]
>
> MonoTouch.UIKit.UIBarButtonItem menuItemBack { get; set; }
>
> And now all UINAvigationBar and UiBarButtonItem aren't recognized anymore. A
> Null is returned by GetNativeField. All ViewControllers in project that are
> still on the old fashioned "template" are ok, like:
>
> [MonoTouch.Foundation.Connect("lblTitle")]
>
> private MonoTouch.UIKit.UINavigationItem lblTitle {
>
> get {
>
> this.__mt_lblTitle =
> ((MonoTouch.UIKit.UINavigationItem)(this.GetNativeField("lblTitle")));
>
> return this.__mt_lblTitle;
>
> }
>
> set {
>
> this.__mt_lblTitle = value;
>
> this.SetNativeField("lblTitulo", value);
>
> }
>
> }
>
> [MonoTouch.Foundation.Connect("menuItemBack")]
>
> private MonoTouch.UIKit.UIBarButtonItem menuItemBack {
>
> get {
>
> this.__mt_menuItemBack =
> ((MonoTouch.UIKit.UIBarButtonItem)(this.GetNativeField("menuItemBack")));
>
> return this.__mt_menuItemBack;
>
> }
>
> set {
>
> this.__mt_menuItemBack = value;
>
> this.SetNativeField("menuItemBack", value);
>
> }
>
> }
>
> So, to test, I edit on Interface Builder an old ViewController, to make
> MonoDevelop to change the code to the new template and GetNativeField stop
> to find. Is there a better or new way to get a control from it's name? All
> other things on screen are OK to find, like buttons and labels.
> Thanks,
> Karl
> _______________________________________________ MonoTouch mailing list
> [email protected]
> http://lists.ximian.com/mailman/listinfo/monotouch

-- 
Michael Hutchinson
http://mjhutchinson.com
_______________________________________________
MonoTouch mailing list
[email protected]
http://lists.ximian.com/mailman/listinfo/monotouch

Reply via email to