我的UIPickerView有问题。我在欧盟AP和NA中有3个值。当我启动该应用程序时,似乎选择了EU,但是当我做出选择时,我NSLog(@"%@", [regions objectAtIndex:row]);
只会返回(null)
,现在当我触摸UIPickerView时,EU值被选中,"EU"
然后从NSLog中返回。
我的问题是:
当用户仅启动应用程序并且什么都不触摸时,如何定义默认值(不仅是标签),该值将被选中。
编辑: 这是我的代码来获取所选项目:
#pragma mark -
#pragma mark PickerView DataSource
- (NSInteger)numberOfComponentsInPickerView:
(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
return [regions count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
return [regions objectAtIndex:row];
}
#pragma mark -
#pragma mark PickerView Delegate
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
selectedRegion = [[NSString alloc] initWithFormat:
@"%@", [regions objectAtIndex:row]];
NSLog(@"%@", selectedRegion);
}
TL:DR版本:
//Objective-C
[self.picker selectRow:2 inComponent:0 animated:YES];
//Swift
picker.selectRow(2, inComponent:0, animated:true)
您没有将选择器设置为选择行(您说您似乎已经完成了,但是无论如何):
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated
或者您没有使用以下方法从选择器中获取所选项目
- (NSInteger)selectedRowInComponent:(NSInteger)component
这将从您的选择器中将选定的行作为整数获取,并根据需要进行操作。这应该可以解决问题。祝好运。
无论如何阅读参考:https:
//developer.apple.com/documentation/uikit/uipickerview
编辑:
在UIPickerView中手动设置和获取所选行的示例:
.h文件:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
{
UIPickerView *picker;
NSMutableArray *source;
}
@property (nonatomic,retain) UIPickerView *picker;
@property (nonatomic,retain) NSMutableArray *source;
-(void)pressed;
@end
.m文件:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize picker;
@synthesize source;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.view.backgroundColor = [UIColor yellowColor];
self.source = [[NSMutableArray alloc] initWithObjects:@"EU", @"USA", @"ASIA", nil];
UIButton *pressme = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, 280, 80)];
[pressme setTitle:@"Press me!!!" forState:UIControlStateNormal];
pressme.backgroundColor = [UIColor lightGrayColor];
[pressme addTarget:self action:@selector(pressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:pressme];
self.picker = [[UIPickerView alloc] initWithFrame:CGRectMake(20, 110, 280, 300)];
self.picker.delegate = self;
self.picker.dataSource = self;
[self.view addSubview:self.picker];
//This is how you manually SET(!!) a selection!
[self.picker selectRow:2 inComponent:0 animated:YES];
}
//logs the current selection of the picker manually
-(void)pressed
{
//This is how you manually GET(!!) a selection
int row = [self.picker selectedRowInComponent:0];
NSLog(@"%@", [source objectAtIndex:row]);
}
- (NSInteger)numberOfComponentsInPickerView:
(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
return [source count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
return [source objectAtIndex:row];
}
#pragma mark -
#pragma mark PickerView Delegate
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
// NSLog(@"%@", [source objectAtIndex:row]);
}
@end
编辑Swift解决方案(来源:Dan Beaulieu的答案)
定义插座:
@IBOutlet weak var pickerView: UIPickerView! // for example
然后,例如,在您的 viewWillAppear 或 viewDidLoad中 ,可以使用以下代码:
pickerView.selectRow(rowMin, inComponent: 0, animated: true)
pickerView.selectRow(rowSec, inComponent: 1, animated: true)
如果检查Swift 2.0框架,则会看到.selectRow定义为:
func selectRow(row: Int, inComponent component: Int, animated: Bool)
单击 Xcode中的.selectRow 选项 ,将显示以下内容:
问题内容: 我需要在活动开始时为ListPreference设置默认值。我试过了,但它使列表的firts条目成为默认条目。我需要它,因为我必须检查一个条件并将满足该条件的值设置为默认值,因此我认为无法从xml文件(带有)中完成此操作 例如,假设我在arrays.xml中具有以下值数组: 在PreferenceScreen xml中: 在活动中,我想做这样的事情: 但这是行不通的,因为它将默认设置为
本文向大家介绍如何设置MySQL默认值NONE?,包括了如何设置MySQL默认值NONE?的使用技巧和注意事项,需要的朋友参考一下 要在MySQL中设置默认值,您需要使用DEFAULT关键字。让我们首先创建一个表- 我们为插入时未输入的值设置了DEFAULT以上。现在,让我们使用insert命令在表中插入一些记录。我们尚未在此处为某些行插入值。默认设置在那里- 使用select语句显示表中的所有记
问题内容: 尝试使用WTForms设置SelectField的默认值时,我像这样将值传递给“默认”参数。 我也尝试了以下方法。 都不将默认选定字段设置为“ Def”。这适用于其他类型的字段,例如TextField。如何设置SelectField的默认值? 问题答案: 你发布的第一种方法是正确的,并且对我有用。唯一无法解释的原因可能是你正在运行旧版本的WTForms,它在1.0.1上对我有效
我注意到,当我在JVM 7和JVM 8上运行JavaFX应用程序时,我得到了不同的默认外观。如何将每个JVM上的默认皮肤设置为相同?
问题内容: 但它不起作用,如何设置默认值? 问题答案: 日期应采用格式。一位数的日期和月份应填充0。一月是01。 从文档中: 代表日期的字符串。 值:RFC3339中定义的有效完整日期,另外具有年份部分为四位或更多位数字表示大于0的数字的资格。 您的代码应更改为:
我在组件中有输入: 模板是: 问题是,如果< code >申请人没有对象< code >代表,申请就会失败。 如果没有,如何设置默认值? 这样地: 我需要始终有< code >类型的< code >代表。 这里还介绍了如何避免错误: