点击完成后(如果有多个选择器),如何在uipickerview中获取当前选定的行

时间:2012-01-31 05:36:29

标签: iphone ios uipickerview

我的应用程序中UIPickerview工作正常。

我想更改UIPickerview的默认选择行为,我的意思是我在UIToolbar中有一个完成按钮。工具栏已添加到UIPickerview

每当用户点击“完成”按钮时,我想将突出显示的条目视为已选择。 这意味着用户可以上下移动,当用户点击“完成”按钮时,我想立即考虑选择

请告诉我,如何在点按“完成”按钮时获取UIPickerview的当前所选行

注意:我的视图中有3个选择器(我的意思是我希望选择pickerview标记和pickerview行)

4 个答案:

答案 0 :(得分:2)

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSString *str = [yourarray objectAtIndex:row];   
}

现在str有一个选定的值。所以按完按钮就可以使用它。

答案 1 :(得分:2)

//假设您已创建了三个选择器视图作为firstPickerView,secondPickerView和ThirdPickerView

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if(pickerView == firstPickerView)
    {
        NSString *strFirstPickerView = [yourarray objectAtIndex:row];
    }
    else if(pickerView == secondPickerView)
    {
        NSString *strSecondPickerView = [yourarray objectAtIndex:row];
    }
    else
    {
        NSString *strThirdPickerView = [yourarray objectAtIndex:row];
    }
}

答案 2 :(得分:2)

selectedRowInComponent:

- Returns the index of the selected row in a given component.
- (NSInteger)selectedRowInComponent:(NSInteger)component

参数 的成分 零索引编号,用于标识选取器视图的组件。 返回值 标识所选行的零索引编号,如果未选择任何行,则为-1。

For Help

为了区分不同的pickerViews,为每个选择器添加标签:

 self.pikcerView.tag = 1;

希望这有帮助。

答案 3 :(得分:0)

以下为UIPickerview控制器的示例

.m文件在这里

#import "SingleComponentPickerViewController.h"
@implementation SingleComponentPickerViewController
@synthesize singlePicker;
@synthesize pickerData;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}

-(IBAction)buttonPressed1
{
    NSInteger row = [singlePicker selectedRowInComponent:0];
    NSString *selected = [pickerData objectAtIndex:row];
    NSString *title = [[NSString alloc] initWithFormat:
                       @"you selected %@!", selected];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                   message : @"Thank you for choosing."
                                                   delegate:nil
                                         cancelButtonTitle :@"Welcome"             
                                         otherButtonTitles :nil];
    [alert show];
    [alert release];
    [title release];
}

- (void)viewDidLoad {


    NSArray *array = [[NSArray alloc] initWithObjects:@"iPhone",@"iPad",@"iPod",@"iMac",@"Mac",
                      @"iBook",@"Safari",nil];
    self.pickerData = array;
    [array release];
    [super viewDidLoad];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
}


- (void)dealloc {
    [singlePicker release];
    [pickerData release];
    [super dealloc];
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
    return [pickerData count];
}

-(NSString *)pickerView:(UIPickerView *)pickerView
            titleForRow:(NSInteger)row
           forComponent:(NSInteger)component 
{
    return[pickerData objectAtIndex:row];
}


@end

和.h文件在这里

#import <UIKit/UIKit.h>

@interface SingleComponentPickerViewController : UIViewController {
    IBOutlet UIPickerView *singlePicker;
    NSArray *pickerData;
}
@property(nonatomic , retain) UIPickerView *singlePicker;
@property(nonatomic , retain) NSArray *pickerData;

-(IBAction)buttonPressed1;

@end
快乐的编码 @samuel