官术网_书友最值得收藏!

Adding Actions to controls

In this recipe, we discuss the concept of Actions and their usage with Xamarin.iOS.

Getting ready

In this recipe, we will discuss how to use Actions with the controls of the user interface.

  1. Create a new iPhone single view application project in Xamarin Studio and name it ButtonInputAction.
  2. Open ButtonInputActionViewController.xib in Interface Builder, and add the same controls, Outlets, and connections as the ones from the project ButtonInput from the previous recipe. Do not add any code in the project for now.

How to do it...

Adding Actions to interface objects is similar to adding Outlets, as follows:

  1. In Interface Builder, press Ctrl and drag from the button to the source code file.
  2. In the context window that will be shown, change the Connection field from Outlet to Action.
  3. Enter OnButtonTap in the Name field, and select Touch Up Inside in the Event field, if it is not already selected.
  4. Click on the Connect button and save the document.
  5. In the ButtonInputActionViewController class, add the following method:
    partial void OnButtonTap(NSObject sender)
    {
    
      this.labelStatus.Text = "Button tapped!";
    
    }

The app is ready! Compile and run it in the simulator. Tap on the button and see the text in the label change, just like in the previous app we created.

How it works...

Actions in Objective-C are the equivalent of control events in C#. They are responsible for delivering notification signals of various objects. In this example, instead of hooking up a handler on the TouchUpInside event of the button, we have added an action for it. As you may already have noticed, the method we added to act as a handler for the action was declared as partial; this is because Xamarin Studio already declared a partial method declaration for us. This is the code that was produced when we saved the document in Interface Builder:

[Action ("OnButtonTap:")]
partial void OnButtonTap (MonoTouch.Foundation.NSObject sender);

The partial declaration of the method is marked with the Action attribute. This is another attribute from the MonoTouch.Foundation namespace that allows us to expose methods as Objective-C Actions. You see that the string parameter passed in the attribute is exactly the same as the action name we entered in Interface Builder, with only an appended colon (:) to it.

Note

Colons in Objective-C indicate the presence of parameters. For example, doSomething is different from doSomething;. The difference is that the first does not accept any parameters, and the second accepts one parameter.

How it works...

The colon at the end of the action name indicates that there is one parameter, in this case, the parameter MonoTouch.Foundation.NSObject sender. This is what our app looks like when we have tapped on the button in the simulator:

There's more...

The example in the preceding section was created just to show how to implement actions in Xamarin.iOS projects. Replacing an event with an action is basically at the discretion of the developer.

See also

  • The Interface Builder, Creating the UI, and Accessing the UI with Outlets recipes
主站蜘蛛池模板: 包头市| 乐安县| 贵定县| 浦城县| 华容县| 太白县| 大兴区| 阳曲县| 将乐县| 神农架林区| 金塔县| 迭部县| 乌拉特前旗| 南康市| 靖宇县| 牙克石市| 祁门县| 五原县| 靖远县| 伊宁市| 忻城县| 章丘市| 古交市| 深水埗区| 陕西省| 突泉县| 札达县| 新化县| 靖远县| 晴隆县| 微山县| 锡林浩特市| 图片| 连山| 蒙阴县| 抚松县| 高州市| 黄陵县| 新源县| 丰台区| 绥阳县|