0

当有人单击该文本字段时,我尝试将文本字段与 Picker 视图相结合,而不是调出默认键盘,我希望它调出一个 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];

}

任何帮助或示例将不胜感激

4

3 回答 3

1

我认为一种简单的方法是在UITextField. 不要让用户单击该文本字段。当用户单击您的文本字段时,您可以显示您的选择器。用户完成拣选后。更新您的文本字段。

于 2012-11-18T11:45:48.117 回答
0

诀窍是永远不要让文本字段成为FirstResponder

将字段子类化,而不是将触摸传递给将调用 becomeFirstResponder 的超级,而是显示选择器视图

于 2012-11-18T11:24:52.957 回答
0

有一个简单的方法,实现 UITextfieldDelegate 协议。这是一个名为的方法

- (BOOL)textFieldShouldBeginEditing:(UITextfield *)textfield

你可以在这个方法上做点什么

return NO;

键盘将不再显示

代码将如下所示

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


      return NO;
}
于 2016-07-30T15:23:53.093 回答