Book Image

iOS Development with Xamarin Cookbook

By : Dimitrios Tavlikos (USD)
Book Image

iOS Development with Xamarin Cookbook

By: Dimitrios Tavlikos (USD)

Overview of this book

Table of Contents (22 chapters)
iOS Development with Xamarin Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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.

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