当有人点击该文本字段时,我尝试使用Picker视图加入textfield,而不是调出默认键盘,我希望它能够显示一个Picker View,其中列出了我喜欢的几个选项。
.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];
}
将提供任何帮助或示例
答案 0 :(得分:1)
我认为一种简单的方法是在UITextField
上方添加透明按钮。不要让用户单击该文本字段。当用户单击您的文本字段时,您可以显示您的选择器。用户完成挑选后。更新文本字段。
答案 1 :(得分:0)
诀窍是永远不要让textfield成为FirstResponder
将Field子类化,而不是将触摸传递给super,它将调用yesFirstResponder,显示选择器视图
答案 2 :(得分:0)
有一种简单的方法,实现UITextfieldDelegate协议。这是一个名为
的方法- (BOOL)textFieldShouldBeginEditing:(UITextfield *)textfield
您可以使用此方法执行某些操作
return NO;
键盘将不再显示
代码如下所示
- (BOOL)textFieldShouldBeginEditing:(UITextfield *)textfield
{
//Do somethings....
return NO;
}