2012-11-18 32 views
0

我試圖加入帶有Picker視圖的文本框,當有人單擊該文本框時,而不是啓動默認鍵盤時,我希望它啓動一個選擇器視圖,其中列出了我喜歡的幾個選擇。ios Picker視圖和文本字段

.h文件中

#import <UIKit/UIKit.h> 

@interface activate : UIViewController<UIPickerViewDataSource, UIPickerViewDelegate> 

@property (weak, nonatomic) IBOutlet UIPickerView *gender; 
@property (strong, nonatomic) NSArray *arrStatus; 

@property (weak, nonatomic) IBOutlet UITextField *GenderTextField; 

@end 

.m文件包含

@synthesize gender; 
@synthesize arrStatus; 
@synthesize GenderTextField; 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.GenderTextField.inputView = gender; 
    arrStatus = [[NSArray alloc] initWithObjects:@"Male", @"Female", nil]; 
} 
-(void)textFieldBeganEditing:(NSNotification *)note{ 
    GenderTextField = note.object; // set ivar to current first responder 
    [gender setHidden:NO]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 
{ 
    //One column 
    return 1; 
} 

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{ 
    //set number of rows 
    return arrStatus.count; 
} 

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
    //set item per row 
    return [arrStatus objectAtIndex:row]; 
} 
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{ 

    self.GenderTextField.text=[arrStatus objectAtIndex:row]; 


} 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.GenderTextField.inputView = self.gender; 
    [gender setHidden:NO]; 

} 

任何幫助或示例將apprciated

+0

可你選秀秀? – sunkehappy

+0

是我的選擇器顯示 –

+0

您面臨的問題是什麼? –

回答

0

訣竅是不要讓文本框becomeFirstResponder

子該字段,而不是通過觸及超級,這將調用beco meFirstResponder,顯示選取器視圖

1

我認爲一個簡單的方法是在UITextField之上添加一個透明按鈕。不要讓用戶點擊該文本字段。當用戶點擊你的文本字段時,你可以顯示你的選擇器。用戶完成採摘後。更新您的文本字段。

0

有一個簡單的方法,實現UITextfieldDelegate協議。有一個名爲

- (BOOL)textFieldShouldBeginEditing:(UITextfield *)textfield 

你可以做一些對這個方法隨後

return NO; 

鍵盤將不再顯示方法

代碼將類似於以下

- (BOOL)textFieldShouldBeginEditing:(UITextfield *)textfield 
{ 
     //Do somethings.... 


     return NO; 
}