`
fosa0989
  • 浏览: 110212 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Input Accessory View (How to add extra controls above keyboard)

 
阅读更多

When building iPhone applications that support or provide text input, it’s often necessary to create some extra buttons (or other controls) beyond the ones provided by the default keyboard interface. Those buttons should be created for specific operations needed by your application, such as moving to the next or previous text field, make the keyboard disappear e.t.c. To understand what exactly this is all about, just see what’s happening when you are using the Safari browser and you type username/password values to login in your e-mail account. Just right above the keyboard, there are two buttons that allow you to move to the next or previous field and a third button that allows you to close the keyboard and see the whole browser again.

That area above the keyboard is an extra UIView, known as Input Accessory View. That view is supported by the UITextField and UITextView as well and is added to any control of that kind using thesetInputAccessoryView method. But that’s the last step. All the controls existsing in the Input Accessory View must be created prior to adding the view to the textfield/textview.


In this post I am going to show how to create an input accessory view programmatically. It’s supposed that you are already familiar writing code in the Xcode and have a basic knowledge on how to create UI controls and views manually. If you don’t, you’ ll learn now.

In this example we will create only two UITextFields. We will set the keyboard to numpad so we won’ t have any way at all to close the keyboard view. All those will be done using the Interface Builder. Then, programmatically we’ ll create two buttons to move between the fields and one more to close the keyboard view. We’ ll add these buttons to a UIView and set the view as our input accessory view that will appear every time that any of our two text fields get the focus (become first responder).

Let’s start. Create a new view-based application project named InputAccViewTest. Open the InputAccViewTestViewController.xib file using the interface builder and add two UITextFields at the top of your view (it should look like the following image):

After that select both of them and set the keyboard to numpad.

It's also necessary to connect every textfield's delegate to the File's Owner object if you want everything to work properly:

Now the real work. Go to the InputAccViewTestViewController.h file and declare the UITextFieldDelegate:

@interface InputAccViewTestViewController : UIViewController

Inside the block now:



@interface InputAccViewTestViewController : UIViewController {
// Declare the all the controls we need. We’ ll have the following:
// The UIView that will become our input accessory view.
// Two IBOutlets for our text fields (the ones created in the IB).
// A UITextField named txtActiveField which shows us the active textfield each time.
// The three buttons that we’ ll put in our input accessory view.
UITextField *txtField1;
UITextField *txtField2;
UITextField *txtActiveField;
UIView *inputAccView;
UIButton *btnDone;
UIButton *btnNext;
UIButton *btnPrev;
}

Now the @property part just right below the closing bracket:



@property (nonatomicretainIBOutlet UITextField *txtField1;
@property (nonatomicretainIBOutlet UITextField *txtField2;
@property (nonatomicretain) UITextField *txtActiveField;
@property (nonatomicretain) UIView *inputAccView;
@property (nonatomicretain) UIButton *btnDone;
@property (nonatomicretain) UIButton *btnNext;
@property (nonatomicretain) UIButton *btnPrev;


Your .h. file should look like this:



#import

@interface InputAccViewTestViewController : UIViewController {
// Declare the all the controls we need. We’ ll have the following:
// The UIView that will become our input accessory view.
// Two IBOutlets for our text fields (the ones created in the IB).
// A UITextField named txtActiveField which shows us the active textfield each time.
// The three buttons that we’ ll put in our input accessory view.
UITextField *txtField1;
UITextField *txtField2;
UITextField *txtActiveField;
UIView *inputAccView;
UIButton *btnDone;
UIButton *btnNext;
UIButton *btnPrev;
}

@property (nonatomicretainIBOutlet UITextField *txtField1;
@property (nonatomicretainIBOutlet UITextField *txtField2;
@property (nonatomicretain) UITextField *txtActiveField;
@property (nonatomicretain) UIView *inputAccView;
@property (nonatomicretain) UIButton *btnDone;
@property (nonatomicretain) UIButton *btnNext;
@property (nonatomicretain) UIButton *btnPrev;

@end


Go to the .m file to @synthesize our objects at the top of the file and after the @implementation statement:

@synthesize txtField1;
@synthesize txtField2;
@synthesize txtActiveField;
@synthesize inputAccView;
@synthesize btnDone;
@synthesize btnNext;
@synthesize btnPrev;

Don’t forget to release all of them in the dealloc method:

- (void)dealloc {
    [txtField1 release];
    [txtField2 release];
    [txtActiveField release];
    [inputAccView release];
    [btnDone release];
    [btnNext release];
    [btnPrev release];
    [super dealloc];
}

Go to the IB and connect the outlets to the UITextFields you created earlier.

Everything until here was more or less known stuff. Now create a method somewhere inside the .m file named createInputAccessoryView as below:


-(void)createInputAccessoryView{
// Create the view that will play the part of the input accessory view.
// Note that the frame width (third value in the CGRectMake method)
// should change accordingly in landscape orientation. But we don’t care
// about that now.
inputAccView = [[UIView allocinitWithFrame:CGRectMake(10.00.0310.040.0)];
// Set the view’s background color. We’ ll set it here to gray. Use any color you want.
[inputAccView setBackgroundColor:[UIColor lightGrayColor]];
// We can play a little with transparency as well using the Alpha property. Normally
// you can leave it unchanged.
[inputAccView setAlpha0.8];
// If you want you may set or change more properties (ex. Font, background image,e.t.c.).
// For now, what we’ ve already done is just enough.
// Let’s create our buttons now. First the previous button.
btnPrev = [UIButton buttonWithTypeUIButtonTypeCustom];
// Set the button’ s frame. We will set their widths to 80px and height to 40px. 
[btnPrev setFrameCGRectMake(0.00.080.040.0)];
// Title.
[btnPrev setTitle@"Previous" forStateUIControlStateNormal];
// Background color.
[btnPrev setBackgroundColor: [UIColor blueColor]];
// You can set more properties if you need to.
// With the following command we’ ll make the button to react in finger tapping. Note that the
// gotoPrevTextfield method that is referred to the @selector is not yet created. We’ ll create it
// (as well as the methods for the rest of our buttons) later.
[btnPrev addTargetself action@selector(gotoPrevTextfieldforControlEvents:UIControlEventTouchUpInside];
// Do the same for the two buttons left.
btnNext = [UIButton buttonWithType:UIButtonTypeCustom];
[btnNext setFrame:CGRectMake(85.0f0.0f80.0f40.0f)];
[btnNext setTitle:@"Next" forState:UIControlStateNormal];
[btnNext setBackgroundColor:[UIColor blueColor]];
[btnNext addTarget:self action:@selector(gotoNextTextfieldforControlEvents:UIControlEventTouchUpInside];
btnDone = [UIButton buttonWithType:UIButtonTypeCustom];
[btnDone setFrame:CGRectMake(240.00.0f80.0f40.0f)];
[btnDone setTitle:@"Done" forState:UIControlStateNormal];
[btnDone setBackgroundColor:[UIColor greenColor]];
[btnDone setTitleColor:[UIColor blackColorforState:UIControlStateNormal];
[btnDone addTarget:self action:@selector(doneTypingforControlEvents:UIControlEventTouchUpInside];
// Now that our buttons are ready we just have to add them to our view.
[inputAccView addSubview:btnPrev];
[inputAccView addSubview:btnNext];
[inputAccView addSubview:btnDone];
}


That method will create the buttons we need and prepare the inputAccView view.
Now implement the UITextField's delegate method textFieldDidBeginEditing. By using that method, we'll know when a textfield becomes first responder (gains the focus), so we can attach the input accessory view.


-(void)textFieldDidBeginEditing:(UITextField *)textField{
// Call the createInputAccessoryView method we created earlier.
// By doing that we will prepare the inputAccView.
[self createInputAccessoryView];
// Now add the view as an input accessory view to the selected textfield.
[textField setInputAccessoryView:inputAccView];
// Set the active field. We' ll need that if we want to move properly
// between our textfields.
txtActiveField = textField;
}


Note the txtActiveField. In that object is stored the selected UITextField every time it has the focus. That field will be used next and will help us to distinct which field is selected and how to move between them.

Inside the createInputAccessoryView method we used three methods: gotoPrevTextfield, gotoNextTextfield and doneTyping that they will trigger every event but they are not yet implemented. Go abovethe createInputAccessoryView method and copy or type the following three methods (the code inside them is quite straightforward and self-explanatory even though I've added some comments as well):


-(void)gotoPrevTextfield{
// If the active textfield is the first one, can't go to any previous
// field so just return.
if (txtActiveField == txtField1) {
return;
}
else {
// Otherwise if the second textfield has the focus, the operation
// of "previous" button can be done and set the first field as the first
// responder.
[txtField1 becomeFirstResponder];
}

// NOTE: If you have more than two textfields, you modify the if... blocks
// according to your needs. The example here is quite simple and in a complete
// app it's possible that you'll have more than two textfields.
}

-(void)gotoNextTextfield{
// If the active textfield is the second one, there is no next so just return.
if (txtActiveField == txtField2) {
return;
}
else {
// Make the second textfield our first responder.
[txtField2 becomeFirstResponder];
}
}

-(void)doneTyping{
// When the "done" button is tapped, the keyboard should go away.
// That simply means that we just have to resign our first responder.
[txtActiveField resignFirstResponder];
}


That's it. Build and run the app. Every time that we tap in a textfield three more buttons will show up along with the keyboard. Play with next, previous or done button. Below is an example on what you should see:

Please note that the buttons in this example are quite simple and of course in a real application you should take care of creating beautiful ones, using images or nice colors. That's up to your imagination. It would be a good idea to use background images for both normal and highlighted state for your buttons if you want to achieve the "animation" that simulates the finger tap.

That's all for the time being. There are more posts that will come (hopefully) soon. In any case, don't hesitate to experiment with everything that I mentioned in this post. I hope it will be useful.

Have a nice iPhone programming day.

分享到:
评论

相关推荐

    object-c源码

    This sample shows how to use a keyboard accessory view. The application uses a single view controller. The view controller's view is covered by a text view. When you tap the text view, the view ...

    react-native-keyboard-accessory:一个React Native键盘附件(视图,导航)组件。 键盘上的粘滞视图

    世博小食游乐场安装通过npm: npm install react-native-keyboard-accessory --save 通过纱线: yarn add react-native-keyboard-accessory用法您可以使用KeyboardAccessoryView或KeyboardAccessoryNavigation组件...

    MFi-Accessory-Specification.rar_MFi_MFi Accessory_accessory_mfi

    《MFi Accessory Specification》这份文档详细阐述了苹果公司对于MFi配件的各项设计、制造和测试要求,是所有想要进入苹果生态系统的第三方配件制造商必须遵循的准则。以下我们将深入探讨这份规范中的关键知识点: ...

    android usb accessory

    标题“android usb accessory”指的是Android设备作为USB附件的功能,它允许Android手机或平板电脑与外部硬件设备通过USB接口进行通信。这种功能使得Android设备能够作为Arduino等微控制器的主机,扩展其控制和交互...

    usb accessory usb从属demo

    USB Accessory USB从属demo是针对USB Human Interface Device (HID) accessory模式的一种示例应用。USB HID accessory模式主要用于非标准设备与Android设备之间的通信,例如游戏控制器、键盘、鼠标等。在这个demo中...

    Accessory-Interface-Specification-R32.pdf

    Accessory Interface Specification Release R32 2019-09-26

    Fuji Accessory Software Setup-ENG

    根据给定的文件信息,我们可以深入探讨Fuji Accessory Software的安装手册,这是一份详尽的指南,旨在帮助用户顺利地在各种环境下部署和配置该软件。以下是从标题、描述、标签以及部分内容中提取的关键知识点: ###...

    Accessory Interface Specification R29.pdf

    本文档为《Accessory Interface Specification R29.pdf》(附件界面规范R29),是苹果公司发布的关于MFi(Made for iPhone/iPod/iPad)的开发文档。MFi程序旨在确保第三方配件与苹果设备(如iPhone、iPad和iPod)的...

    Accessory Authentication_accessory_

    AccessoryauthenticationprovidesamechanismforAppledevicestotrusttheidentitiesandfeaturesetsof accessoriesthatarecompliantwiththisspecification....

    Accessory Interface Specification 32.pdf

    根据提供的文件信息,本文档是关于苹果公司的《Accessory Interface Specification 32》,这是一个适用于iOS开发者的技术规范,特别涉及到MFi认证和开发。本文档涉及Lightning接口的技术说明,以及与之相关的各种...

    Accessory Interface Specification 最新解密版

    《Accessory Interface Specification 最新解密版》是针对苹果设备的配件接口规范文档,具有至关重要的参考价值。文档详细阐述了配件接口的规范内容、使用原则、测试指南、开发工具、适配器与代理以及各种兼容性问题...

    linux cluster howto

    - **辅助硬件(Miscellaneous/accessory hardware)**:例如网络交换机、路由器等,确保集群内部及与外部网络的有效连接。 - **整体硬件组装(Putting-it-all-together hardware)**:描述如何将各个组件合理地组合在...

    Fuji Accessory Software Setup for 2003 Server-ENG

    根据给定文件的信息,我们可以详细地探讨一下在Windows Server 2003上使用Fuji Accessory Software时需要注意的关键知识点。 ### 使用Fuji Accessory Software的注意事项 #### 1.1 使用带有Internet Explorer增强...

    MFi Accessory Firmware Specification R45.pdf

    《MFi Accessory Firmware Specification R45》是Apple公司针对其MFi(Made for iPhone/iPod/iPad)认证配件制定的固件规范文档。这份文档详细阐述了第三方开发者和制造商如何设计、开发和验证与Apple设备兼容的硬件...

    用于ios的mfi认证资料,Accessory Interface Specification R31

    《用于iOS的MFi认证资料:Accessory Interface Specification R31详解》 苹果的MFi(Made for iPhone/iPod/iPad)认证是针对第三方配件制造商的一项重要标准,旨在确保配件与苹果设备之间的兼容性和安全性。...

    Fuji Accessory Software Setup for 2003 Server-JPN.

    ### Fuji Accessory Software 在 Windows Server 2003 上的安装与配置 #### 概述 本文档旨在为用户在Windows Server 2003环境下使用Fuji Accessory Software提供必要的指导。考虑到软件运行时可能会遇到的问题,...

    苹果配件开发,《Accessory Interface Specification R22无水印,全书签》,内涵iap2开发协议

    标题《Accessory Interface Specification R22无水印,全书签》,描述《苹果配件开发,《Accessory Interface Specification R22》,内涵iap2开发协议》,以及标签“Apple accessory”表明这份文档是关于苹果配件...

Global site tag (gtag.js) - Google Analytics