tags:

views:

232

answers:

3

Hey all,

I'm a C# programmer dabbling in a bit of iPhone development using MonoTouch.

I add a new View Interface Definition to my project and double click to open it up in Interface Builder. I add a UIButton. I save the file, and inspect the xib.designer.cs file, and I can see no reference to the new button.

I downloaded the code from http://monotouchexamples.com/ where I could see an example of autogenerated code behind :

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

I opened up MainWindow.xib in interface builder. I notice a few differences. File's Owner is of type UIApplication instead of NSObject. What is the importance of this? There is an App Delegate object of type AppDelegate. I can't add an AppDelegate to my own view, or at least I can't find it in the Library. Do I need to add one? I can see that the existing controls on MainWindow.xib have Referencing Outlets to the App Delegate. I add a new button and I want to hook it up. When I click and drag a New Referencing Outlet to the App Delegate a context menu appears that lists the existing controls. How do I add a new element to this list, or where does this list come from?

I've been spoilt by the Visual Studio world where I just dump a button on a form and start writing code for the click event. Could someone provide some pointers about the steps needed to get this working on MonoTouch?

Thanks,

Patrick

+1  A: 

Adding a button by itself is not enough. The button is not accessible outside the Interface Builder. You need add an Outlet, and connect the button with the outlet in Interface Builder.

Remember: Outlets are the members in your Controller class that get a reference to the controls, you can't just access the controls without them.

Dave Van den Eynde
Thanks for the reply. Where and how do I add an outlet? Is this something I have to do in code? Could you highlight where the outlets have been added in this project : http://github.com/chrisntr/Monotouch-Examples/tree/master/MonotouchExamples/ ?
Patrick J Collins
The outlets are created in Interface Builder, where exactly depends on what version of Mac OS X you are running. I suggest you take a look at a tutorial for Monotouch.
Dave Van den Eynde
A: 

Hey Patrick,

It sounds like the project I created is out of date - I remember there were quite a few changes around how the generated buttons are created in the designer file. I will update the project soon for you.

As Dave said, to get the code to be auto generated you need to add an outlet with Interface Builder. There should be an example on this video here - http://bit.ly/aWoItN but the server seems to be down at the moment.

Hope this helps,

ChrisNTR

chrisntr
+1  A: 

As Dave says, you need to add an outlet to your controller class, and connect your button to that outlet, before any auto-generated code will appear. This caught me out too initially.

You choose your controller class in the Interface Builder library window, choose 'outlets' in the bottom part of the library, and add an outlet there. You then need to select your button, choose the connections tab of the inspector window, and drag from the "New referencing outlet" circle over to your controller class. Interface Builder will then prompt you to choose an outlet to connect to. Then when you save, you should get the auto-generated code appear in the .xib.designer.cs file, and then you should be able to reference your button via that outlet variable in your .xib.cs file.

NeilDurant