2012-10-11 100 views
1

我嘗試實施customkeyboard用的UISearchBar。首先我所有mainclass.h在那裏我使用的UISearchBar是這個樣子自定義鍵盤與UISearchBar的實現?

#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一些。可一個指導我怎麼做this.thanx

+0

你有沒有找到解決這個呢? –

回答

1

您需要設置UITextFieldinputViewUISearchBar是爲了獲取用戶輸入使用。您可以通過搜索搜索欄的子視圖並找到文本字段來完成此操作。添加到您的mainclassviewDidLoad方法:

for (UIView *view in searchBar.subviews) { 
    if ([view isKindOfClass: [UITextField class]]) { 
     UITextField *textField = (UITextField *)view; 
     textField.inputView = // Your custom keyboard (PashtoKeyboard) 
     break; 
    } 
} 
+1

感謝您的好解決方案 –

+0

不客氣! – lnafziger