使用UISearchBar自定义键盘实现?

时间:2012-10-11 15:46:10

标签: iphone ios xcode keyboard uisearchbar

我尝试用UISearchBar实现自定义键盘。首先我使用UISearchBar的所有mainclass.h看起来像这样

#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

现在我的mainclass.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中

#import <UIKit/UIKit.h>
@interface SearchBar : UISearchBar {
}
@property (readwrite, retain) UIView *inputView;
@end
SearchBar.m中的

#import "SearchBar.h"
@implementation SearchBar
@synthesize inputView;
- (void)dealloc {
[super dealloc];
}
@end

然后最后我有PashtoKeyboard.h和PashtoKeyboard.m我创建我的自定义键盘,由于大编码我没有在这里显示这些类代码我用textveiw和textfield测试它。 但它不能与UISearchBar一起使用。有人可以指导我如何做到这一点.thanx

1 个答案:

答案 0 :(得分:1)

您需要设置inputView正在使用的UITextField的{​​{1}}才能获得用户输入。您可以通过搜索搜索栏的子视图并查找文本字段来完成此操作。将其添加到UISearchBar的<{1}}方法:

viewDidLoad