点击“完成”时如何获取 uipickerview 中当前选定的行(具有多个选择器)

发布于 2025-01-01 02:39:53 字数 398 浏览 2 评论 0原文

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

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

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

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

注意:我的视图中有 3 个选择器(我的意思是我想要 pickerview 标签和 pickerview 选定的行)

I have got UIPickerview in my application working fine.

I want to change the default selection behavior of the UIPickerview, i mean i have a done button in the UIToolbar. The toolbar is added to the UIPickerview.

Whenever the user taps on the "Done" button, I want to consider the highlighted entry as selected.
That means user can move up and down , when the user taps on the "Done" button, i want to consider the selection now

Please let me know , how can I fetch the currently selected row for the UIPickerview when tapped "Done" button

Note: I have 3 pickers in my view ( i mean i want to have pickerview tag and pickerview selected row)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

没有你我更好 2025-01-08 02:39:54

//假设您创建了三个选取器视图,分别为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];
    }
}

//Suppose you have created three picker views as firstPickerView, secondPickerView and 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];
    }
}
酒几许 2025-01-08 02:39:54

selectedRowInComponent:

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

参数
组件
标识选取器视图组件的零索引数字。
返回值
标识所选行的零索引数字,如果没有选择行,则为 -1。

寻求帮助

并制作不同 pickerView 之间的差异为每个选择器添加标签:

 self.pikcerView.tag = 1;

希望这有帮助。

selectedRowInComponent:

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

Parameters
component
A zero-indexed number identifying a component of the picker view.
Return Value
A zero-indexed number identifying the selected row, or -1 if no row is selected.

For Help

And to make a difference between different pickerViews add tag to each of the pickers:

 self.pikcerView.tag = 1;

Hope this helps.

过期以后 2025-01-08 02:39:54
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSString *str = [yourarray objectAtIndex:row];   
}

现在 str 有一个选定的值。这样您就可以在按下按钮后使用它。

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

now str have a selected value. so you can use it when done button press.

给妤﹃绝世温柔 2025-01-08 02:39:54

以下是 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

祝您编码愉快
@塞缪尔

Following example for the UIPickerview controller

.m files here

#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

and .h file here

#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

happy coding
@samuel

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文