3
votes

I installed XCode, MonoMac, Mono and MonoDevelop on a Mac OS X machine, and I cant for the love of me figure out how to successfully wire up a Click event to a simple button.

First, I control-dragged my button to the header file, and it created an outlet. Now, I can access my button (btnHello) from C# code, thats cool - but how, and where, do I bind events to it?

I tried doing

this.btnHello.PerformClick += delegate {
    this.Title = "Hello World";
};

but MonoDevelop complains about I cant do that, due to some "method group" stuff.

I have been researching this for days, and I cant find any tutorials on how to do this, and I dont want to use sample code as it does not give me the process of how to do this properly in the future. I tried looking at the MonoTouch getting started guide, since it is close to MonoMac, but that did not help either.

Any help would be great, especially regarding outlets and actions, and how I can wire them up to my elements! Thank you!

3

3 Answers

3
votes

Try:

    this.btnHello.Activated += delegate {
    this.Title = "Hello World";
};

This code should go somewhere in your Controller, most likely in AwakeFromNib.

2
votes

Most of the issue probably stems from the fact that most tutorials you find on the web still reference older versions of MonoDevelop, XCode and Interface Builder.

I've posted this link previously and it shows how to get a basic project going, including event handlers with the new XCode 4.2+ setup.

http://tufnelltech.blogspot.ca/2012/01/hello-os-x-from-c-five-steps.html

0
votes

NSButton.PerformClick() is a method, not an event, so you can't connect a delegate to it.

I'm not familiar enough with the Mac APIs to suggest how to do what you want, unfortunately.