当有人单击该文本字段时,我尝试将文本字段与 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];
}
任何帮助或示例将不胜感激