热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何设置UIPickerView的默认值

如何设置UIPick

//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
@interface ViewController : UIViewController
{
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


定义插座:

@IBOutlet weak var pickerView: UIPickerView! // for example

然后,例如,在您的 viewWillAppearviewDidLoad中 ,可以使用以下代码:

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 ,将显示以下内容:

如何设置UIPickerView的默认值





推荐阅读
author-avatar
Hello_DavidLeete_811
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有