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

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
主站蜘蛛池模板: 江都市| 平塘县| 旬阳县| 周口市| 旺苍县| 安岳县| 同心县| 镇宁| 东乡县| 株洲县| 灵武市| 芜湖县| 通辽市| 如皋市| 乌兰浩特市| 普格县| 蓝田县| 津市市| 盘锦市| 洛川县| 若尔盖县| 张家界市| 洛川县| 哈尔滨市| 连城县| 甘南县| 曲周县| 股票| 永吉县| 绵竹市| 温州市| 裕民县| 青田县| 开阳县| 诏安县| 忻州市| 澄城县| 新乡市| 锡林浩特市| 宝应县| 许昌县|