UIKit allows applications to substitute custom input views for the system keyboard. It also enables applications to have an accessory view above the system keyboard or custom input view. Additionally, it enables applications to play key-click sounds when users tap on a controls of an input view or input accessory view.

Input Views and Input Accessory Views

The UIKit framework includes support for custom input views and input accessory views. Your application can substitute its own input view for the system keyboard when users edit text or other forms of data in a view. For example, an application could use a custom input view to enter characters from a runic alphabet. You may also attach an input accessory view to the system keyboard or to a custom input view; this accessory view runs along the top of the main input view and can contain, for example, controls that affect the text in some way or labels that display some information about the text.

To get this feature if your application is using <a href="http://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextView_Class/Reference/UITextView.html#//apple_ref/occ/cl/UITextView" target="_self">UITextView</a> and <a href="http://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextField_Class/Reference/UITextField.html#//apple_ref/occ/cl/UITextField" target="_self">UITextField</a> objects for text editing, simply assign custom views to the <a href="http://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextField_Class/Reference/UITextField.html#//apple_ref/occ/instp/UITextField/inputView" target="_self">inputView</a> and <a href="http://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextField_Class/Reference/UITextField.html#//apple_ref/occ/instm/UITextField/inputAccessoryView" target="_self">inputAccessoryView</a> properties. Those custom views are shown when the text object becomes first responder.

You are not limited to input views and input accessory views in framework-supplied text objects. Any class inheriting directly or indirectly from <a href="http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIResponder_Class/Reference/Reference.html#//apple_ref/occ/cl/UIResponder" target="_self">UIResponder</a> (usually a custom view) can specify its own input view and input accessory view. The UIResponder class declares two properties for input views and input accessory views:

When the responder object becomes the first responder and <a href="http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIResponder_Class/Reference/Reference.html#//apple_ref/occ/instm/UIResponder/inputView" target="_self">inputView</a> (or <a href="http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIResponder_Class/Reference/Reference.html#//apple_ref/occ/instp/UIResponder/inputAccessoryView" target="_self">inputAccessoryView</a>) is not nil, UIKit animates the input view into place below the parent view (or attaches the input accessory view to the top of the input view). The first responder can reload the input and accessory views by calling the <a href="http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIResponder_Class/Reference/Reference.html#//apple_ref/occ/instm/UIResponder/reloadInputViews" target="_self">reloadInputViews</a> method of UIResponder.

The UITextView class redeclares the <a href="http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIResponder_Class/Reference/Reference.html#//apple_ref/occ/instp/UIResponder/inputView" target="_self">inputView</a> and <a href="http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIResponder_Class/Reference/Reference.html#//apple_ref/occ/instp/UIResponder/inputAccessoryView" target="_self">inputAccessoryView</a> properties as readwrite. Clients of UITextView objects need only obtain the input and input-accessory views—either by loading a nib file or creating the views in code—and assign them to their properties. Custom view classes (and other subclasses that inherit from UIResponder) should redeclare one or both of these properties and their backing instance variables and override the getter method for the property—that is, don’t synthesize the properties’ accessor methods. In their getter-method implementations, they should return it the view, loading or creating it if it doesn’t already exist.

You have a lot of flexibility in defining the size and content of an input view or input accessory view. Although the height of these views can be what you’d like, they should be the same width as the system keyboard. If UIKit encounters an input view with an <a href="http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/c/econst/UIViewAutoresizingFlexibleHeight" target="_self">UIViewAutoresizingFlexibleHeight</a> value in its autoresizing mask, it changes the height to match the keyboard. There are no restrictions on the number of subviews (such as controls) that input views and input accessory views may have. For more guidance on input views and input accessory views, see iOS Human Interface Guidelines.

To load a nib file at run time, first create the input view or input accessory view in Interface Builder. Then at runtime get the application’s main bundle and call <a href="http://developer.apple.com/library/ios/documentation/UIKit/Reference/NSBundle_UIKitAdditions/Introduction/Introduction.html#//apple_ref/occ/instm/NSBundle/loadNibNamed:owner:options:" target="_self">loadNibNamed:owner:options:</a> on it, passing the name of the nib file, the File’s Owner for the nib file, and any options. This method returns an array of the top-level objects in the nib, which includes the input view or input accessory view. Assign the view to its corresponding property. For more on this subject, see “Nib Files” in Resource Programming Guide.

Listing 7-1 illustrates a custom view class lazily creating its input accessory view in the inputAccessoryView getter method.

Listing 7-1 Creating an input accessory view programmatically

The subviews of an input view and input accessory view can be anything you want. If they are buttons or other controls, you need to specify targets and actions for each control and implement the associated action methods to perform data input or manipulation.

Just as it does with the system keyboard, UIKit posts <a href="http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIWindow_Class/UIWindowClassReference/UIWindowClassReference.html#//apple_ref/c/data/UIKeyboardWillShowNotification" target="_self">UIKeyboardWillShowNotification</a>, <a href="http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIWindow_Class/UIWindowClassReference/UIWindowClassReference.html#//apple_ref/c/data/UIKeyboardDidShowNotification" target="_self">UIKeyboardDidShowNotification</a>, <a href="http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIWindow_Class/UIWindowClassReference/UIWindowClassReference.html#//apple_ref/c/data/UIKeyboardWillHideNotification" target="_self">UIKeyboardWillHideNotification</a>, and <a href="http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIWindow_Class/UIWindowClassReference/UIWindowClassReference.html#//apple_ref/c/data/UIKeyboardDidHideNotification" target="_self">UIKeyboardDidHideNotification</a> notifications. The object observing these notifications can get geometry information related to the input view and input accessory view and adjust the edited view accordingly. See “Keyboards and Input Methods” for examples and related information.

Playing Input Clicks

Starting in iOS 4.2, you can play standard system keyboard clicks when a user taps in your custom input views and keyboard accessory views. First, adopt the UIInputViewAudioFeedback protocol in your input view. Then, call the <a href="http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDevice_Class/Reference/UIDevice.html#//apple_ref/occ/instm/UIDevice/playInputClick" target="_self">playInputClick</a> method when responding to a key tap in the view.

Adopting the UIInputViewAudioFeedback Protocol

Perform the following three steps to adopt the <a href="http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIInputViewAudioFeedback_ProtocolReference/Reference/Reference.html#//apple_ref/occ/intf/UIInputViewAudioFeedback" target="_self">UIInputViewAudioFeedback</a> protocol:

  1. In your Xcode project, create a subclass of the <a href="http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/cl/UIView" target="_self">UIView</a> class. In the header file, indicate that the subclass conforms to the UIInputViewAudioFeedback protocol, as follows:
  2. In the implementation file for your UIView subclass, implement the <a href="http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIInputViewAudioFeedback_ProtocolReference/Reference/Reference.html#//apple_ref/occ/intfm/UIInputViewAudioFeedback/enableInputClicksWhenVisible" target="_self">enableInputClicksWhenVisible</a> method, as follows:
  3. Finally, in the Interface Builder document for your custom input or accessory view, select the View object. In the Identity inspector, set the class for the object to be your UIView subclass.

Playing Input Clicks

To play an input click for a key tap in a custom input or keyboard accessory view, first ensure that view adopts the <a href="http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIInputViewAudioFeedback_ProtocolReference/Reference/Reference.html#//apple_ref/occ/intf/UIInputViewAudioFeedback" target="_self">UIInputViewAudioFeedback</a> protocol as described in “Adopting the UIInputViewAudioFeedback Protocol.” Then, for each tap that you want to provide a click sound for, call the <a href="http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDevice_Class/Reference/UIDevice.html#//apple_ref/occ/instm/UIDevice/playInputClick" target="_self">playInputClick</a> method of the <a href="http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDevice_Class/Reference/UIDevice.html#//apple_ref/occ/cl/UIDevice" target="_self">UIDevice</a> class, as follows:


Leave a Reply

电子邮件地址不会被公开。 必填项已用*标注