views:

729

answers:

3

I'm new to MonoTouch and iPhone development. I have my images (PNG) in a resources folder in MonoDevelop, however if I want to set the image property for a button in Interface Builder, how do I do that? It's always a blank dropdown. Do I need to use XCode to access the XIB file and then somehow embed the button image file I'll need in it?

+5  A: 

This is a known limitation of MonoDevelop and Interface Builder. To add images to an XIB in Interface Builder they must be part of an XCode project, which of course coming from MonoDevelop they're not.

To achieve what you're trying to do you will need to set the image via code, and ensure the build action of your image is set to Content. To do this, simply right click your image inside MonoDevelop, and select Build Action > Content.

On your view with the button on it, create an outlet in Interface Builder for your button, hook it up, then from code to set your image, you just need to use the .FromFile("path/name") method of UIImage.

UIImage buttonImage = UIImage.FromFile ("resources/image.png");
myButton.SetBackgroundImage (buttonImage,UIControlState.Normal);

That's off the top of my head, but I think that should do it.

Ira Rainey
You can still set the image in Interface Builder, they simply won't show up in IB while you're editing. Add the image to the root dir in monotouch, set as content as above, and in IB set the image to "myimage.png". You'll see a question mark during editing, but during runtime the image will appear.
Eduardo Scoz
+2  A: 

You can manually set the image in Interface Builder, but it wont show up until run time. The image name can include a path, e.g. "images/settings.png".

jamie
+1  A: 

I also needed this to work, here is a workaround I found. You need to create a dummy xcode project. Place it in the same folder as your project. Add all your xib files and image files to that xcode project by dragging them in when the project is opened in xcode. Now you will be able to see the preview of the images. The image files must be in the same folder as the project file and xcode must be opened with the dummy project while the interface builder is opened. Not great - but solves the issue for now. Here is a sample project I have created.

For this issue to be resolved on the MonoDevelop side - some inter-process communication code needs to be created, I think that a good starting point will be looking for "PBXProjectWatcherServerConnection-3.1.2" in google.

Assaf R.