问题描述
我尝试使用UISearchBar实现customkeyboard.首先,我使用UISearchBar的mainclass.h如下
I try to implement customkeyboard with UISearchBar .first of all my mainclass.h where i use UISearchBar is look like this
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@class PashtoKeyboard;
@class SearchBar;
@interface MainViewController : UIViewController<UISearchBarDelegate > {
IBOutlet SearchBar *searchBar;
PashtoKeyboard *pashtoKeyboard;
}
@property(nonatomic,retain) PashtoKeyboard *pashtoKeyboard;
@property (nonatomic,retain) SearchBar *searchBar;
@end
现在是我的主班.m
#import "MainViewController.h"
#import "PashtoKeyboard.h"
#import "SearchBar.h"
@implementation MainViewController
@synthesize pashtoKeyboard,searchBar;
- (void)viewDidLoad
{
[super viewDidLoad];
//Create Pashto Keyboard
PashtoKeyboard *pashtoKey = [[PashtoKeyboard alloc] initWithFrame:CGRectMake(0.0, 460.0-216.0, 320.0, 216.0)];
[pashtoKey loadView];
//[self.view addSubview:hebKey];
[pashtoKey addTarget:self action:@selector(pashtoKeyboard_clicked:) forControlEvents:1001];
[pashtoKey addTarget:self action:@selector(pashtoKeyboardBackspace_clicked:) forControlEvents:1002];
[pashtoKey addTarget:self action:@selector(pashtoKeyboardEnter_clicked:) forControlEvents:1003];
[self setPashtoKeyboard:pashtoKey];
//[hebKey setHidden:YES];
[pashtoKey release];
searchBar.inputView = pashtoKey;
searchBar.delegate=self;
searchBar.userInteractionEnabled=YES;
}
#pragma mark Pashto Keyboard Methods
- (void) pashtoKeyboard_clicked:(id)sender{
[searchBar setText:[pashtoKeyboard keyboardText]];
}
- (void) pashtoKeyboardBackspace_clicked:(id)sender{
[searchBar setText:[pashtoKeyboard keyboardText]];
}
- (void) pashtoKeyboardEnter_clicked:(id)sender{
[searchBar setText:[pashtoKeyboard keyboardText]];
}
在SearchBar.h
In SearchBar.h
#import <UIKit/UIKit.h>
@interface SearchBar : UISearchBar {
}
@property (readwrite, retain) UIView *inputView;
@end
在SearchBar.m
in SearchBar.m
#import "SearchBar.h"
@implementation SearchBar
@synthesize inputView;
- (void)dealloc {
[super dealloc];
}
@end
然后最后我有了PashtoKeyboard.h和PashtoKeyboard.m,我在其中创建我的自定义键盘,由于编码大,我在这里没有显示这些类代码,而是使用textveiw和textfield测试了它们.但是它不能与UISearchBar一起使用.可以有人指导我怎么做.谢谢
And then finally i have PashtoKeyboard.h and PashtoKeyboard.m where i create my custom keyboard,due to large coding i not show these classes code here i tested its with textveiw and textfield.But it not works with UISearchBar .can some one guide me how to do this.thanx
推荐答案
您需要设置UISearchBar
使用的UITextField
的inputView
,以获取用户输入.您可以通过在搜索栏的子视图中搜索并找到文本字段来完成此操作.将此添加到mainclass
的viewDidLoad
方法中:
You need to set the inputView
of the UITextField
that the UISearchBar
is using in order to get the user input. You can do this by searching through the subviews of the search bar and finding the text field. Add this to the viewDidLoad
method of your mainclass
:
for (UIView *view in searchBar.subviews) {
if ([view isKindOfClass: [UITextField class]]) {
UITextField *textField = (UITextField *)view;
textField.inputView = // Your custom keyboard (PashtoKeyboard)
break;
}
}
这篇关于使用UISearchBar实现Customkeyboard?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!