Xcode:使一个文本字段带来一个选择器查看或下拉菜单 [英] Xcode: Make A Text Field Bring Up A Picker View or Drop Down Menu
问题描述
选项1:
我想通过Interface Builder创建一个文本字段,当有人点击该文本字段,而不是打开默认键盘,我希望它启动一个选择器视图,它列出了我喜欢的几个选择。一旦用户完成选择某个值,他们可以通过选择器视图点击完成按钮,并且选择器视图将消失,文本字段将被填充到您在选择器视图上选择的内容。
选项2
如果以前的方法需要太多的代码来完成,有人可以提供关于如何创建基本的下拉菜单的示例代码类似于网站上的标准下拉菜单?
谢谢
创建新文件。我打电话给我的AEMPicker。 .h:
@protocol AEMPickerDelegate< NSObject>
- (void)toucPicker:(NSString *)string;
@optional
- (void)setInitialPickerValueToRow:(int)i inComponent:(int)j动画:(BOOL)k;
@end
@interface AEMPicker:UIViewController< UIPickerViewDataSource,UIPickerViewDelegate> {
UIPickerView * pickerView;
}
@property(nonatomic,strong)NSArray * contentArray;
@property(nonatomic,assign)id< AEMPickerDelegate> delegatePicker;
- (id)initWithArray:(NSArray *)内容inFrame:(CGRect)pickerFrame;
@end
.m:
#importAEMPicker.h
@implementation AEMPicker
@synthesize contentArray;
@synthesize delegatePicker;
- (id)initWithArray:(NSArray *)内容inFrame:(CGRect)pickerFrame
{
self = [super init];
if(self){
contentArray = [NSArray arrayWithArray:contents];
pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.delegate = self;
[self.view addSubview:pickerView];
}
return self;
}
- (void)setInitialPickerValueToRow:(int)i inComponent:(int)j animated:(BOOL)k {
[pickerView selectRow:i inComponent:j animated:k];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [contentArray count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow :( NSInteger)行forComponent:(NSInteger)组件{
return [contentArray objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
[self.delegatePicker touchPicker:[contentArray objectAtIndex :行]];
}
@end
现在,在课堂上想要呈现你的pickerView,在 #import AEMPicker;
添加到你的.h:
@interface YourClass:UIViewController< AEMPickerDelegate,UITextFieldDelegate> {
AEMPicker * picker;
UIPopoverController * pickerPopOver;
UIPopoverController * pOC;
CGRect popRect;
}
添加到.m:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField resignFirstResponder];
popRect = CGRectMake(406,110,0,0);
CGRect pickerRect = CGRectMake(0,10,0,0);
NSArray * contents = [[NSArray alloc] initWithObjects:@Object 1,@Object 2,@Object 3,nil];
picker = [[AEMPicker alloc] initWithArray:content inFrame:pickerRect];
picker.delegatePicker = self;
pickerPopOver = [[UIPopoverController alloc] initWithContentViewController:picker];
pickerPopOver.popoverContentSize = CGSizeMake(320,250);
[pickerPopOver presentPopoverFromRect:popRect inView:self.view allowedArrowDirections:UIPopoverArrowDirectionAny animated:TRUE];
pOC = pickerPopOver;
}
- (void)toucPicker:(NSString *)string {
[yourTextField setText:string];
[pickerPopOver dismissPopoverAnimated:YES];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
[pOC dismissPopoverAnimated:NO];
返回YES;
}
这应该给你一个漂亮的基本选择器popover来玩。 >
Can someone please provide some example code on how I could create the following features on an iOS app:
Option 1: I want to create a text field through Interface Builder, and when someone clicks on that text field, instead of bringing up the default keyboard, I want it to bring up a Picker View which lists several choices of my liking. Once the user is done picking a certain value, they can click on a "done" button by the picker view and the picker view will go away and the Text Field will be populated with what they chose on the Picker View.
Option 2 If the previous method will require too much code to accomplish, could someone provide example code on how to create a basic Drop Down menu similar to how a standard drop down menu on a website?
Thanks
Create a new file. I called mine AEMPicker. The .h:
@protocol AEMPickerDelegate <NSObject>
-(void)touchedPicker:(NSString *)string;
@optional
-(void)setInitialPickerValueToRow:(int)i inComponent:(int)j animated:(BOOL)k;
@end
@interface AEMPicker : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>{
UIPickerView *pickerView;
}
@property (nonatomic, strong) NSArray *contentArray;
@property (nonatomic, assign) id<AEMPickerDelegate> delegatePicker;
- (id)initWithArray:(NSArray *)contents inFrame:(CGRect)pickerFrame;
@end
the .m:
#import "AEMPicker.h"
@implementation AEMPicker
@synthesize contentArray;
@synthesize delegatePicker;
- (id)initWithArray:(NSArray *)contents inFrame:(CGRect)pickerFrame
{
self = [super init];
if (self) {
contentArray = [NSArray arrayWithArray: contents];
pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.delegate = self;
[self.view addSubview:pickerView];
}
return self;
}
-(void)setInitialPickerValueToRow:(int)i inComponent:(int)j animated:(BOOL)k{
[pickerView selectRow:i inComponent:j animated:k];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [contentArray count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [contentArray objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
[self.delegatePicker touchedPicker:[contentArray objectAtIndex:row]];
}
@end
Now, in the class you want to present your pickerView, after #import AEMPicker;
add to your .h:
@interface YourClass : UIViewController <AEMPickerDelegate, UITextFieldDelegate>{
AEMPicker *picker;
UIPopoverController *pickerPopOver;
UIPopoverController *pOC;
CGRect popRect;
}
add to your .m:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField resignFirstResponder];
popRect = CGRectMake(406, 110, 0, 0);
CGRect pickerRect = CGRectMake(0, 10, 0, 0);
NSArray *contents = [[NSArray alloc] initWithObjects:@"Object 1", @"Object 2", @"Object 3", nil];
picker = [[AEMPicker alloc] initWithArray:contents inFrame:pickerRect];
picker.delegatePicker = self;
pickerPopOver = [[UIPopoverController alloc] initWithContentViewController:picker];
pickerPopOver.popoverContentSize = CGSizeMake(320, 250);
[pickerPopOver presentPopoverFromRect:popRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:TRUE];
pOC = pickerPopOver;
}
-(void)touchedPicker:(NSString *)string{
[yourTextField setText:string];
[pickerPopOver dismissPopoverAnimated:YES];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
[pOC dismissPopoverAnimated:NO];
return YES;
}
That should give you a pretty basic picker popover to play with.
这篇关于Xcode:使一个文本字段带来一个选择器查看或下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!