ios - 如何设置 UIPickerView 的默认值

标签 ios objective-c xcode swift uipickerview

我的 UIPickerView 有问题。 我在其中有 3 个值 EU AP 和 NA。 当我启动应用程序时,EU 似乎已被选中,但是当我制作 NSLog(@"%@", [regions objectAtIndex:row]); 我只返回 (null), 现在,当我触摸 UIPickerView 时,EU 值被选中,我从 NSLog 中返回 "EU"

我的问题是:

当用户只启动应用程序并且什么都不碰时,我如何定义一个默认值(不仅仅是标签)。

编辑:这是我获取所选项目的代码:

#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 显示如下:

关于ios - 如何设置 UIPickerView 的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11777072/

相关文章:

c++ - 我如何使用 boost::test 和 xcode 4 来测试一些 ios c++ 代码?

ios - 使用 xCode 登录 Facebook 的 Phonegap 3.0 应用程序

ios - 通过 Xcode 中的多个 Storyboard segues 传递数据

iphone - Objective-C 中的内存管理问题

iphone - 当显示 UILocalnotification 时,如何在后台创建或设置设备锁定

Xcode - 无法采用协议(protocol)

iphone - EXC_BAD_ACCESS 错误.. textfield resignFirstResponder

ios - 从ttf文件中获取字体

iphone - 如何将 CCSpriteFrame 转换为 CCTexture2D (Cocos2d)

objective-c - 如何使用 MasterDetail 应用程序模板更新 DetailView