1

我尝试使用 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

4

1 回答 1

1

您需要设置正在使用inputView的,以便获取用户输入。您可以通过搜索搜索栏的子视图并找到文本字段来执行此操作。将此添加到您的方法中:UITextFieldUISearchBarviewDidLoadmainclass

for (UIView *view in searchBar.subviews) {
    if ([view isKindOfClass: [UITextField class]]) {
        UITextField *textField = (UITextField *)view;
        textField.inputView = // Your custom keyboard (PashtoKeyboard)
        break;
    }
}
于 2013-05-13T23:15:54.197 回答