如何设置UIPickerView的默认值

Chr*_*ris 63 xcode objective-c uipickerview ios swift

我的UIPickerView有问题.我在EU AP和NA中有3个值.当我启动应用程序欧盟似乎被选中但是当我做一个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);


}
Run Code Online (Sandbox Code Playgroud)

Tot*_*mus 93

TL:DR版本:

//Objective-C
[self.picker selectRow:2 inComponent:0 animated:YES];
//Swift
picker.selectRow(2, inComponent:0, animated:true)
Run Code Online (Sandbox Code Playgroud)

要么你没有设置你的选择器来选择行(你说你似乎已经做过但无论如何):

- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated
Run Code Online (Sandbox Code Playgroud)

或者您没有使用以下方法从选择器中获取所选项目

- (NSInteger)selectedRowInComponent:(NSInteger)component
Run Code Online (Sandbox Code Playgroud)

这将从您的选择器中将所选行作为整数获取,并随意使用它.这应该为你做的伎俩.祝好运.

无论如何阅读参考: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
Run Code Online (Sandbox Code Playgroud)

.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
Run Code Online (Sandbox Code Playgroud)

编辑Swift解决方案(来源:Dan Beaulieu的回答)

定义出口:

@IBOutlet weak var pickerView: UIPickerView!  // for example
Run Code Online (Sandbox Code Playgroud)

然后在viewWillAppearviewDidLoad中,您可以使用以下内容:

pickerView.selectRow(rowMin, inComponent: 0, animated: true)
pickerView.selectRow(rowSec, inComponent: 1, animated: true)
Run Code Online (Sandbox Code Playgroud)

如果你检查Swift 2.0框架,你会看到.selectRow定义为:

func selectRow(row: Int, inComponent component: Int, animated: Bool) 
Run Code Online (Sandbox Code Playgroud)

选项单击 .selectRow在Xcode中显示以下内容:


Har*_*rji 18

这是如何设置UIPickerView的默认值

[self.picker selectRow:4 inComponent:0 animated:YES];
Run Code Online (Sandbox Code Playgroud)

  • 当它就像这一行一样简单时,不知道上面为什么需要上述所有内容...... (5认同)

Dan*_*ieu 9

Swift解决方案:

定义出口:

@IBOutlet weak var pickerView: UIPickerView!  // for example
Run Code Online (Sandbox Code Playgroud)

然后在viewWillAppearviewDidLoad中,您可以使用以下内容:

pickerView.selectRow(rowMin, inComponent: 0, animated: true)
pickerView.selectRow(rowSec, inComponent: 1, animated: true)
Run Code Online (Sandbox Code Playgroud)

如果您检查Swift 2.0框架,您将看到.selectRow定义为:

func selectRow(row: Int, inComponent component: Int, animated: Bool) 
Run Code Online (Sandbox Code Playgroud)

选项单击 .selectRow在Xcode中显示以下内容:

在此输入图像描述


小智 6

我也有这个问题.但显然存在方法调用顺序的问题.你必须打电话:

[self.picker selectRow:2 inComponent:0 animated:YES];
Run Code Online (Sandbox Code Playgroud)

打电话

[self.view addSubview:self.picker];
Run Code Online (Sandbox Code Playgroud)


Per*_*rve 5

您必须 - (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated 在它出现之前发送到选取器视图。文档指出方法 selectedRowInComp... 将给出 -1,因此选取器视图可能处于没有选定行的状态。事实证明,它在创建时就处于这种状态。