Xcode4 Xcode:使文本字段显示选择器视图或下拉菜单
有人可以提供一些示例代码,说明我如何在iOS应用程序上创建以下功能: 选项1: 我想通过Interface Builder创建一个文本字段,当有人单击该文本字段时,我希望它显示一个选择器视图,其中列出我喜欢的几个选项,而不是显示默认键盘。一旦用户完成选取某个值,他们可以单击选取器视图旁的“完成”按钮,选取器视图将消失,文本字段将填充他们在选取器视图上选择的内容 选项2 如果前面的方法需要太多的代码才能完成,有人能提供一些示例代码,说明如何创建一个基本的下拉菜单,类似于如何在网站上创建一个标准的下拉菜单吗Xcode4 Xcode:使文本字段显示选择器视图或下拉菜单,xcode4,ios5,uitextview,uipickerview,drop-down-menu,Xcode4,Ios5,Uitextview,Uipickerview,Drop Down Menu,有人可以提供一些示例代码,说明我如何在iOS应用程序上创建以下功能: 选项1: 我想通过Interface Builder创建一个文本字段,当有人单击该文本字段时,我希望它显示一个选择器视图,其中列出我喜欢的几个选项,而不是显示默认键盘。一旦用户完成选取某个值,他们可以单击选取器视图旁的“完成”按钮,选取器视图将消失,文本字段将填充他们在选取器视图上选择的内容 选项2 如果前面的方法需要太多的代码才能完成,有人能提供一些示例代码,说明如何创建一个基本的下拉菜单,类似于如何在网站上创建一个标准的下
谢谢创建一个新文件。我打电话给我的提货员。A.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
现在,在类中,在#import-AEMPicker代码>添加到您的.h:
@interface YourClass : UIViewController <AEMPickerDelegate, UITextFieldDelegate>{
AEMPicker *picker;
UIPopoverController *pickerPopOver;
UIPopoverController *pOC;
CGRect popRect;
}
这将为您提供一个非常基本的picker popover,供您使用。“文本字段将不会填充他们在picker视图中选择的内容。”。。。您的意思是文本字段将被填充吗?Xcode正在popRect上调用“使用未声明的标识符”,但是我不知道popRect应该是什么,所以您是否介意添加popRect声明,或者解释发生此错误的原因?抱歉--添加CGRect popRect
到实现选择器的类的.h文件。我对答案进行了编辑以反映这一点。非常感谢,现在一切似乎都正常工作,但是我的选择器似乎没有在选择器中当前选定的值上显示蓝色高亮显示的选择器,尽管仍在选择中心的值。你知道为什么吗?事实上,我不知道。在我的实现中显示良好。picker视图正常工作,但会出现一条消息:-[UIPopoverController presentPopoverFromRect:inView:permittedArrowDirections:animated:]:传递到此方法的rect的宽度和高度必须非零。这将是未来版本中的一个例外。我已经改变了,例如=>popRect=CGRectMake(10,10320,120);错误信息现在消失了。我使用:Xcode 4.6和iOS SDK 6.1
@interface YourClass : UIViewController <AEMPickerDelegate, UITextFieldDelegate>{
AEMPicker *picker;
UIPopoverController *pickerPopOver;
UIPopoverController *pOC;
CGRect popRect;
}
- (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;
}