Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do IBOutlets in MonoTouch?

I have done one iPhone app in Objective-C. When I want to link a Label to some data in that I would declare it like this:

@interface CityDetailViewController : UIViewController {
UILabel *cityName;
}

@property(nonatomic, retain) IBOutlet UILabel *cityName;

And then when the CityDetailViewController object is created in code I would set the city name like this [self.cityView.cityName setText:city.name];

I can't, for the life of me, figure out how to do this in MonoTouch. I tried manually creating the Outlets through Interface Builder and I tried adding this code that I found in the .designer.cs file from another project:

[MonoTouch.Foundation.Connect("headlineLabel")]
    private MonoTouch.UIKit.UILabel headlineLabel {
        get {
            return ((MonoTouch.UIKit.UILabel)(this.GetNativeField("headlineLabel")));
        }
        set {
            this.SetNativeField("headlineLabel", value);
        }
    }

And I've tried a combination of both of those things. They don't work. The closest I can get is to actually apply the Outlet using Interface Builder, but when my View is declared I get this error:

this class is not key value coding-compliant for the key headlineLabel.

So I'm completely at a loss. Can someone explain this to me please?

like image 562
James P. Wright Avatar asked Dec 03 '22 13:12

James P. Wright


1 Answers

Pretty sure that I've figured it out so I'll post the process here for others to look at:

Step 1) Start with your empty View like this: step 1
(source: jamespwright.com)

Step 2) Add a UILabel Control to your View: step 2
(source: jamespwright.com)

Step 3) Select "File's Owner": step 3
(source: jamespwright.com)

Step 4) Select the Identity Inspector (CMD + 4) and add a new Class Outlet: step 4
(source: jamespwright.com)

Step 5) Select the Connections Inspector (CMD + 2) and you should see your new Class Outlet there: step 5
(source: jamespwright.com)

Step 6) Drag the connection to your Label on your View.

Step 7) Inside your code you populate it by typing myLabel.Text = "My Label!"

Hopefully that will help someone else out in the future.

like image 153
James P. Wright Avatar answered Dec 06 '22 01:12

James P. Wright