Confusingly with events, delegates, etc. In Monotouch

I come from a very experienced Windows.NET background, and I'm learning with Monotouch, and I'm very confused about how to respond to events. I like to keep things simple, and I read the Montouch tutorials and looked at examples. I am embarrassed about how to respond to events.

Suppose I have a ViewController with UIButton and UILabel. When I click the button, I want to change the shortcut to say "Press the button."

So I could just do the following:

public override void ViewDidLoad () { base.ViewDidLoad (); this.btnClickMe.TouchUpInside += (sender, e) => { this.lblOutput.Text = "Clicked @ " + DateTime.Now.ToShortTimeString (); } 

OR, alternatively, I could use this approach, which I think will serve me better when it comes to answering buttons pressed in NavigationBars, etc. In IB, I Ctrl-Drag create an action. Then I move the [Action] method to the .cs file and do the following.

 [Action ("btnClickMe_TouchUpInside:")] public void btnClickMe_TouchUpInside (NSObject sender) { this.lblOutput.Text = "Clicked @ " + DateTime.Now.ToShortTimeString (); } 

What makes it more confusing for me is that some user interface components have a .delegate member. What can I add an event to.

What is the best method, or am I completely confused? If so, you can direct me to where I can learn best practices, the right approach, etc.

Many thanks

Mike

+4
source share
1 answer

For a good introduction, I recommend the Xamarin article on Events, Protocols, and Delegates .

Regarding your question about connecting Action Outlets to relay output connection events, they might say the following:

The main difference between using .NET events as opposed to target actions is that the latter allows you to connect multiple controls to a single action.

So, you may be inclined to take an approach to action if you build a quick and dirty calculator, and by pressing numbers 0-9, everyone does the same - you can just read the number from the button. (If you have ever used VB to create a control array, you could familiarize yourself with this technique.)

However, I believe that my personal preference is to stick to the links. This requires less thought and the transition between MonoDevelop and Interface Builder. I take the UISlider and reference it, then I have access to all its properties and events from the code. Thus, intercepting something later can be done from the code. Many developers have commented that they deviate from using IB ...

Returning to the facts, MonoTouch offers several redundant ways to interact with the user interface. For those coming from Objective-C, you will find familiar "Objective-C delegates" and links to "selectors". Coming from C #, you can often ignore these approaches. But you should take the time to read this article so that you are familiar with the language used in the Apple documentation. For example, it is important to note the difference between "Objective-C delegates" and "C # delegates."

Delegates are used for callbacks in iOS in the same way that .NET uses Events. To make the iOS API and the way Objective-C delegates use more like .NET, MonoTouch provides .NET events in many places where delegates are used on iOS.

In time, I believe you will find the API flexible and adaptable for .NET templates. The conversion between NS types and .NET types is often transparent (for example, from a lambda expression to NSAction).

Let me know if you need more specific information. Greetings.

+5
source

All Articles