如何在基于 FirstPickerView 选定值的 SecondPickerView 中显示值?
how to Show Value in a SecondPickerView based on FirstPickerView Selected Value?
我有 2 个选择器视图:statePicker
和 providerPicker
。我已经为两个选择器视图填充了数组的值(stateNames
代表 statePicker
,providerNames
代表 providerPicker
)。
我的问题是,我必须根据 statePicker
的 selected 值在 providerPicker
中显示一个值。例如,如果 select Tamilnadu
in statePicker
意味着 providerPicker
自动应该显示 TNEB
。同样,KSEB
对应 Karnataka
。
这是我的代码:
DetailDeviceViewController.h
#import <UIKit/UIKit.h>
@interface DeviceDetailViewController : UIViewController<UIPickerViewDelegate, UIPickerViewDataSource>
@property(strong,nonatomic) UIPickerView *statePicker;
@property(strong,nonatomic) UIPickerView *providerPicker;
@property (strong, nonatomic) NSArray *stateNames;
@property (strong, nonatomic) NSArray *providerNames;
@end
DetailDeviceViewController.m
#import "DeviceDetailViewController.h"
@interface DeviceDetailViewController ()
@end
@implementation DeviceDetailViewController
@synthesize stateNames,providerNames,statePicker,providerPicker;
- (void)viewDidLoad
{
[super viewDidLoad];
//Array declaration
stateNames = [[NSArray alloc]initWithObjects:@"Tamilnadu",@"Karnataka", nil];
providerNames =[[NSArray alloc]initWithObjects:@"TNEB",@"KSEB",nil];
//statePicker
statePicker = [[UIPickerView alloc]initWithFrame:CGRectMake(50, 50, 100, 200)];
[statePicker setDelegate:self];
[statePicker setDataSource:self];
statePicker.showsSelectionIndicator =YES;
[self.view addSubview:statePicker];
//providerPicker
providerPicker = [[UIPickerView alloc]initWithFrame:CGRectMake(50, 150, 100, 200)];
[providerPicker setDelegate:self];
[providerPicker setDataSource:self];
providerPicker.showsSelectionIndicator =YES;
[self.view addSubview:providerPicker];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
if([pickerView isEqual:statePicker])
{
return 1;
}
else
{
return 1;
}
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if([pickerView isEqual:statePicker])
{
return [stateNames count];
}
else
{
return [providerNames count];
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if ([pickerView isEqual:statePicker])
{
return [stateNames objectAtIndex: row];
}
else
{
return [providerNames objectAtIndex: row];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
您可以使用 PickerView
的 didSelectRow
方法。
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (pickerView == self.statePicker)
{
NSString *strFirstPickerView = [Statearray objectAtIndex:row];//declare this string globally
[providerPicker selectRow:row inComponent:0 animated:YES];
[providerPicker reloadAllComponents];
}
else
{
if(strFirstPickerView.length==0)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"ROFL"
message:@"Please select state first."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}
}
UIPickerViewDelegate
协议包含方法:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
选中一行时,将调用此方法。
当您 select 选择器中的值时,此委托方法将自动调用。
所以在这个方法中你可以写你想执行的。
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// YOUR CODE HERE.
}
我有 2 个选择器视图:statePicker
和 providerPicker
。我已经为两个选择器视图填充了数组的值(stateNames
代表 statePicker
,providerNames
代表 providerPicker
)。
我的问题是,我必须根据 statePicker
的 selected 值在 providerPicker
中显示一个值。例如,如果 select Tamilnadu
in statePicker
意味着 providerPicker
自动应该显示 TNEB
。同样,KSEB
对应 Karnataka
。
这是我的代码:
DetailDeviceViewController.h
#import <UIKit/UIKit.h>
@interface DeviceDetailViewController : UIViewController<UIPickerViewDelegate, UIPickerViewDataSource>
@property(strong,nonatomic) UIPickerView *statePicker;
@property(strong,nonatomic) UIPickerView *providerPicker;
@property (strong, nonatomic) NSArray *stateNames;
@property (strong, nonatomic) NSArray *providerNames;
@end
DetailDeviceViewController.m
#import "DeviceDetailViewController.h"
@interface DeviceDetailViewController ()
@end
@implementation DeviceDetailViewController
@synthesize stateNames,providerNames,statePicker,providerPicker;
- (void)viewDidLoad
{
[super viewDidLoad];
//Array declaration
stateNames = [[NSArray alloc]initWithObjects:@"Tamilnadu",@"Karnataka", nil];
providerNames =[[NSArray alloc]initWithObjects:@"TNEB",@"KSEB",nil];
//statePicker
statePicker = [[UIPickerView alloc]initWithFrame:CGRectMake(50, 50, 100, 200)];
[statePicker setDelegate:self];
[statePicker setDataSource:self];
statePicker.showsSelectionIndicator =YES;
[self.view addSubview:statePicker];
//providerPicker
providerPicker = [[UIPickerView alloc]initWithFrame:CGRectMake(50, 150, 100, 200)];
[providerPicker setDelegate:self];
[providerPicker setDataSource:self];
providerPicker.showsSelectionIndicator =YES;
[self.view addSubview:providerPicker];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
if([pickerView isEqual:statePicker])
{
return 1;
}
else
{
return 1;
}
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if([pickerView isEqual:statePicker])
{
return [stateNames count];
}
else
{
return [providerNames count];
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if ([pickerView isEqual:statePicker])
{
return [stateNames objectAtIndex: row];
}
else
{
return [providerNames objectAtIndex: row];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
您可以使用 PickerView
的 didSelectRow
方法。
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (pickerView == self.statePicker)
{
NSString *strFirstPickerView = [Statearray objectAtIndex:row];//declare this string globally
[providerPicker selectRow:row inComponent:0 animated:YES];
[providerPicker reloadAllComponents];
}
else
{
if(strFirstPickerView.length==0)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"ROFL"
message:@"Please select state first."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}
}
UIPickerViewDelegate
协议包含方法:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
选中一行时,将调用此方法。
当您 select 选择器中的值时,此委托方法将自动调用。
所以在这个方法中你可以写你想执行的。
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// YOUR CODE HERE.
}