这是我到目前为止所做的截图:

所以我要做的就是当你选择"选择一个名字"Textfield我需要一个Picker来显示,输入@"Jack".
一个简短的问题:在注册过程中,我想要求用户从值列表中选择一个值.
这是使用视图控制器添加所有文本字段和值的选择器视图的正确方法吗?由于选择器视图在文本字段区域之间需要这么大的空间,我想知道在这种情况下最佳实践是什么?
到目前为止这是我的代码:
class RegisterViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource{
@IBOutlet weak var gradeTextField: UITextField!
@IBOutlet weak var gradePicker: UIPickerView!
let gradePickerValues = ["5. Klasse", "6. Klasse", "7. Klasse"]
func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int{
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
return gradePickerValues.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return gradePickerValues[row]
}
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int){
gradeTextField.text = gradePickerValues[row]
self.view.endEditing(true)
}
override func viewDidLoad() {
super.viewDidLoad() …Run Code Online (Sandbox Code Playgroud) 我想更改UIPickerView的边框颜色.我没有看到UIPickerView的色调属性.有什么办法可以做到吗?还是一个解决方法?
谢谢.
我一直在试图诊断我的应用程序无法使用在旧版XCode下成功运行的代码自动预选UIPickerView的最后一行.我认为这是Xcode而不是iOS 6中的一个错误,因为我在iOS 6设备上运行的旧应用程序正常运行,但重新编译Xcode 4.5.2下的代码行为不正常.我已经将以下示例放在一起来演示问题,但在我提交错误报告之前,我希望本论坛上的其他人能够确定问题是否与我的代码有关,或者确实是Xcode/iOS中的错误.
我创建了一个单一的视图应用程序,并使用导航控制器和两个IBOutlet设置故事板,一个到UILabel,我在那里显示所选行,一个到UIPickerView.
这是我的自定义视图控制器的头文件:
#import <UIKit/UIKit.h>
@interface DisposableViewController : UIViewController
<UIPickerViewDataSource, UIPickerViewDelegate>
@end
Run Code Online (Sandbox Code Playgroud)
这是我的自定义视图控制器的实现文件:
#import "DisposableViewController.h"
const NSInteger MAX_VALUE = 10;
@interface DisposableViewController ()
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
@property (weak, nonatomic) IBOutlet UILabel *selectedValueLabel;
@end
@implementation DisposableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated
{
[self.pickerView reloadAllComponents];
[self.pickerView selectRow:MAX_VALUE inComponent:0 animated:NO];
self.selectedValueLabel.text = [NSString stringWithFormat:@"%d", [self.pickerView selectedRowInComponent:0]];
}
//**
//** UIPickerViewDataSource methods
//**
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{ …Run Code Online (Sandbox Code Playgroud) 我试图用UIToolBar显示UIPickerView,但收到一些错误.
这是我的代码 -
CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height-216-44, 320, 44);
CGRect datePickerTargetFrame = CGRectMake(0, self.view.bounds.size.height-216, 320, 216);
UIView *darkView = [[UIView alloc] initWithFrame:self.view.bounds];
darkView.alpha = 0;
darkView.backgroundColor = [UIColor blackColor];
darkView.tag = 9;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissDatePicker:)];
[darkView addGestureRecognizer:tapGesture];
[self.view addSubview:darkView];
UIDatePicker *picker = [[UIDatePicker alloc] init];
picker.autoresizingMask = UIViewAutoresizingFlexibleWidth;
picker.datePickerMode = UIDatePickerModeDate;
[picker addTarget:self action:@selector(dueDateChanged:) forControlEvents:UIControlEventValueChanged];
[picker setFrame:CGRectMake(0,235,320,120)];
picker.backgroundColor = [UIColor blackColor];
[self.view addSubview:picker];
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 44)];
toolBar.tag = 11; …Run Code Online (Sandbox Code Playgroud) 下面的代码将更改所有3个组件的选择器视图的字体颜色.然而,当我试图旋转车轮时,它会崩溃.我认为它与didSelectRow函数有关.也许这两个函数必须以某种方式嵌套?任何的想法?
func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
var attributedString: NSAttributedString!
if component == 0 {
attributedString = NSAttributedString(string: a.text!, attributes: [NSForegroundColorAttributeName : UIColor.redColor()])
}
if component == 1 {
attributedString = NSAttributedString(string: b.text!, attributes: [NSForegroundColorAttributeName : UIColor.redColor()])
}
if component == 2 {
attributedString = NSAttributedString(string: c.text!, attributes: [NSForegroundColorAttributeName : UIColor.redColor()])
}
return attributedString
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){
switch component {
case 0:
aOutput.text = a[row] --> **Code …Run Code Online (Sandbox Code Playgroud) 我在viewDidLoad方法中编写了以下代码:
categoryPickerView=[[UIPickerView alloc]init];
categoryPickerView.alpha = 0;
[self.view addSubview:categoryPickerView];
categoryPickerView.delegate=self;
categoryPickerView.tag=1;
Run Code Online (Sandbox Code Playgroud)
并调用此方法来隐藏选择器视图
- (IBAction)hidePickerView:(id)sender {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.6];
CGAffineTransform transfrom = CGAffineTransformMakeTranslation(0, 200);
categoryPickerView.transform = transfrom;
categoryPickerView.alpha = categoryPickerView.alpha * (-1) + 1;
[UIView commitAnimations];
}
Run Code Online (Sandbox Code Playgroud)
我的问题是我想在选择器视图上显示"完成"按钮,选择器视图应隐藏在按钮单击上.
我有一个值为5的整数,我想用这个索引启动我的UIPickerView,现在该怎么办?通常UIPickerView在默认的0索引行上,但我希望它在用户定义的索引上,可以是5,6或任何其他数字.
我的通用应用程序有一个设置页面,其中用户必须从十几个可用主题中选择一个主题.
问题是,如果我使用选择器视图,它会占用iPhone的大部分屏幕.
我认为我想要的就像你的标准Windows下拉菜单,它只是在一行显示当前选择,然后点击它扩展为可滚动的可能项目列表.选择项目会将列表折叠回原始表单.
所以我的问题是:处理这种情况的最佳方法是什么?谁能指点我成功处理这种情况的图片或代码示例?
编辑:这是我正在寻找的行为:
即在功能上等同于下拉列表.
如何使用SDK 7 UIPickerView 在iOS 7上设置背景颜色,并在iOS 5和6上使用标准选择器?默认情况下,它在iOS 7上是透明的.
uipickerview ×10
ios ×8
iphone ×2
objective-c ×2
swift ×2
ios6 ×1
ios7 ×1
uitableview ×1
uitextfield ×1
uiview ×1
xcode ×1
xcode4.5 ×1
xcode6gm ×1