
[英]how can place multiple arrays in uipickerview components and when selected row in one component how can get the array in another component
[英]How to fetch the currently selected row in the uipickerview when tapped Done ( having multiple pickers)
我的应用程序中的UIPickerview
运行正常。
我想更改UIPickerview
的默认选择行为,我的意思是我在UIToolbar
有一个完成按钮。 工具栏将添加到UIPickerview
。
每当用户点击“完成”按钮时,我都希望选中突出显示的条目。 这意味着用户可以上下移动,当用户点击“完成”按钮时,我现在要考虑选择
请让我知道,点击“完成”按钮后,如何获取UIPickerview
当前选择的行
注意:我的视图中有3个选择器(我的意思是我想有pickerview标记和pickerview选中的行)
selectedRowInComponent:
- Returns the index of the selected row in a given component.
- (NSInteger)selectedRowInComponent:(NSInteger)component
参数component零索引数字,标识选择器视图的组成部分。 返回值零索引的数字,标识选定的行;如果未选择任何行,则为-1。
为了使不同的pickerView有所不同,请向每个picker添加标签:
self.pikcerView.tag = 1;
希望这可以帮助。
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSString *str = [yourarray objectAtIndex:row];
}
现在str具有选定的值。 因此您可以在完成按钮按下后使用它。
//假设您已经创建了三个选择器视图,分别是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];
}
}
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.