In our last installment we introduced the Xamarin iOS Designer and briefly discussed using it to build “scenes” in iOS Storyboards. This time around we are going to take a quick look at how we use the Xamarin iOS Designer to make UI widgets accessible from our code. Let’s start by fleshing out our main scene a bit for a simple hello world with adjustable text size:
Our storyboard scene contains a UILabel for the Hello World message, a UISlider to adjust the text size, a UITextField and a UIButton. We are going to need to manipulate the UILabel at the top when the text size slider is used or when the “Say Hello” button is touched, so this widget will need to be accessible from code.
When using Xcode’s Interface Builder with either xib’s or storyboards, creating “code behind” for a widget or event involves and somewhat tricky process of splitting your editor and dragging a widget or event from the visual storyboard into the proper implementation file within the code editor. Xamarin’s iOS Designer does things in a somewhat simpler fashion. Simply give the widget (the UILabel in this case) in the Identity section of the UILabel properties, and the Xamarin iOS designer will generate the proper “outlet” that will enable this control to be accessed with code.
We will also create an outlet for the UITextField so that our code can retrieve the value input by the user. We can now do something similar in order to wire up events, called Actions in Interface Builder. Like before, with the Xamarin iOS Designer there is no need to CTRL-drag lines between visual and code editors, just type in a new event handler name or choose an existing one.
Once we have done this for the UISlider as well, we can take a look at the “code behind” file that has been generated by Xamarin.iOS, in this case it is called StoryboardSampleViewController.designer.cs. This file is maintained by the designer, there will be one for each view controller in the storyboard, and it should not be edited manually. A partial class has been declared that defines the outlets (control references) and actions (events) that you have defined in the designer.
For the events, or actions, we must manually implement the handler in the StoryboardSampleViewController.cs file. We can use some help from code completion here, just type “partial ” and you will be presented with a code completion for the handlers we have configured in the storyboard.
Finally, we might implement our event handlers something like the following:
That’s it! Look Ma, no Xcode. In Part 3 we will learn to add additional scenes (view controllers) to our storyboard and a quick Xamarin iOS Designer trick for creating the skeleton view controller code.
Latest posts by Falafel Posts (see all)
- Matching Complex Query String Rewrite Rule in IIS - March 22, 2017
- Disable Content Filters in Sitefinity - March 8, 2017
- On Sitefinity Custom Widget Caching - February 22, 2017
- Dynamic Content Detail Widget Templates in Sitefinity - February 8, 2017
- Using Google Services in UWP C# Apps – Part 2 - February 7, 2017