iOS Development with Xamarin Cookbook
上QQ阅读APP看书,第一时间看更新

Displaying and editing text

In this recipe, we will learn how to display simple text blocks with editing functionality.

Getting ready

In this recipe, we will discuss the usage of UITextView and how to display editable text with it. Create a new iPhone Single View Application project in Xamarin Studio and name it TextViewApp.

How to do it...

Perform the following steps:

  1. Open TextViewAppViewController.xib in Interface Builder.
  2. Add a UIButton near the top of its view and a UITextView below it. Connect both objects to their outlets.
  3. Save the document.
  4. Back in Xamarin Studio, enter the following ViewDidLoad method in the TextViewAppViewController class:
    public override void ViewDidLoad ()
    {
      base.ViewDidLoad ();
      this.buttonFinished.Enabled = false;
      this.buttonFinished.TouchUpInside += (sender, e) => {
    
        this.myTextView.ResignFirstResponder();
    
      } ;
      this.myTextView.Delegate = new MyTextViewDelegate(this);
    }
  5. Add the following nested class:
    private class MyTextViewDelegate : UITextViewDelegate
    {
    
      public MyTextViewDelegate (TextViewAppViewController parentController)
      {
        this.parentController = parentController;
      }
      private TextViewAppViewController parentController;
    
      public override void EditingStarted (UITextView textView)
      {
        this.parentController.buttonFinished.Enabled = true;
      }
    
      public override void EditingEnded (UITextView textView)
      {
        this.parentController.buttonFinished.Enabled = false;
      }
    
      public override void Changed (UITextView textView)
      {
        Console.WriteLine ("Text changed!");
      }
    
    }
  6. Compile and run the app on the simulator. Tap somewhere in the text view and the keyboard will appear. Type some text and then tap on the Finished button to hide the keyboard.

How it works...

The UITextView class provides an object that displays editable blocks of text. To respond to the events of our text view, we have implemented a class (shown in the following code) that inherits from UITextViewDelegate, which will act as the text view's delegate:

private class MyTextViewDelegate : UITextViewDelegate
{
  public MyTextViewDelegate (TextViewAppViewController parentController)
  {this.parentController = parentController;}
  private TextViewAppViewController parentController;

We declared a constructor that accepts a TextViewAppViewController object so that we can have the instance of our controller available to access our controls.

Then, we override three methods of the UITextViewDelegate class, as shown in the following code:

public override void EditingStarted (UITextView textView)
{
  this.parentController.buttonFinished.Enabled = true;
}

public override void EditingEnded (UITextView textView)
{
  this.parentController.buttonFinished.Enabled = false;
}

public override void Changed (UITextViewtextViewUITextView textView)
{
  Console.WriteLine ("Text changed!");
}

These methods are the handlers that will get called whenever a corresponding event is triggered. When tapping on the text view, the EditingStarted method gets called. We enable the Finished button in it. When we type some text in the text view, the Changed method gets called, and we can see the output of the Console.WriteLine method in Xamarin Studio's Application Output pad. Finally, when we tap on the Finished button, the keyboard hides, and the EditingEnded method gets called. This method allows us to disable the button.

In the ViewDidLoad method, we assign a handler to the TouchUpInside event of the button, as shown in the following code:

this.buttonFinished.TouchUpInside += (sender, e) => {
  this.myTextView.ResignFirstResponder ();
};

We call the text view's ResignFirstResponder() method in it so that when the button is tapped, the text view will lose focus, causing the keyboard to hide. Then, we assign a new instance of the delegate we created to the text view's Delegate property, passing the instance of the TextViewAppViewController object, as shown in the following code:

this.myTextView.Delegate = new MyTextViewDelegate (this);

There's more...

Delegates in Objective-C are somewhat different than those in C#. Although in both worlds, their most common usage is to provide access to some form of event notification mechanism, in Objective-C, this mechanism is a bit more complex. A C# delegate is much like a function pointer in C or C++ programming languages. It is an object that holds a reference to a method of a specific signature. On the other hand, an Objective-C delegate is a certain type of object that conforms to a specific protocol. It is basically an object that wraps one or more methods (and/or other members) that act as event handlers.

Note

An Objective-C protocol is similar to an interface in C#.

The concept of delegate objects might seem confusing at first, but it is not difficult to comprehend. Regarding the event notification mechanism, Xamarin.iOS simplifies things for .NET developers by providing events for most objects, including UITextView described here.

See also

  • The Using the keyboard recipe