Lorenzo B
Lorenzo B

Reputation: 33428

Monotouch: Create IBOutlet and Actions Programmatically

I need to create IBOutlet and IBActions without using Interface Builder. How can I do?

Upvotes: 3

Views: 1590

Answers (2)

Geoff Norton
Geoff Norton

Reputation: 5056

I'm not sure how you're planning on connecting an IBOutlet or an IBAction without a backing nib, but you can create the needed bits manually.

IBActions are just interface builders way of decorating a selector contract with an implementation. You can manually create a method bound to a selector with the following code example:

[Export ("someMethod:")]
public void SomeMethod (int arg) {
}

IBOutlets are just interface builders way of decorating instance variables (ivars). Ivars are exposed by the monotouch code generator as properties, so that we can lazily query the native proxy backing store and dont need to marshal the value at every transition point. You can manually create a instance variable on the proxy class with the following code example:

[Connect("varname")]
private NSObject varname {
    get {
        return ((NSObject) (this.GetNativeField("varname")));
    }
    set {
        this.SetNativeField("varname", value);
    }
}

Astute readers will notice that the MT code generator generates slightly different code:

private NSObject __mt_varname;
[Connect("varname")]
private NSObject varname {
    get {
        this.__mt_varname = ((NSObject) (this.GetNativeField("varname")));
            return this.__mt_varname;
    }
    set {
            this.__mt_varname = value;
        this.SetNativeField("varname", value);
    }
}

If you dangle any state off the object you are storing in the native field, you will need to use this construct as well, so the garbage collector sees the reference.

Upvotes: 2

dalexsoto
dalexsoto

Reputation: 3412

So i guess you want to go XIB less, you have 2 options

Adding everything by hand

or use Miguel's MonoTouch.Dialog

This 2 articles will set you up on either way.

Alex

Upvotes: 1

Related Questions