Ios 三相关选择器视图
我需要创建三个独立的pickerView。第一个pickerView是categoryPickerView。选择某个值时,应加载其他两个pickerView(productPickerview和modelPickerview)未正确加载数据 正在触发didSelectRow回调,但my NSLog显示,它没有通过第一个if语句检查它是否等于“Audio” 对于catogeryPickerView,我在viewDidLoad中为其编写了以下数组:-Ios 三相关选择器视图,ios,iphone,objective-c,Ios,Iphone,Objective C,我需要创建三个独立的pickerView。第一个pickerView是categoryPickerView。选择某个值时,应加载其他两个pickerView(productPickerview和modelPickerview)未正确加载数据 正在触发didSelectRow回调,但my NSLog显示,它没有通过第一个if语句检查它是否等于“Audio” 对于catogeryPickerView,我在viewDidLoad中为其编写了以下数组:- devicecatogery=[[NSArra
devicecatogery=[[NSArray alloc]initWithObjects:@"Audio",@"Video", nil];
//code for numeric keypad done button
UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 44, 320, 300)];
numberToolbar.barStyle = UIBarStyleBlackTranslucent;
numberToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:@"Apply" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)],
nil];
[numberToolbar sizeToFit];
// catogery pickerview
categoryPickerView=[[UIPickerView alloc]init];
categoryPickerView.delegate=self;
categoryPickerView.tag=1;
txtCategory.inputView=categoryPickerView;
txtCategory.inputAccessoryView=numberToolbar;
//product pickerview
productPickerView=[[UIPickerView alloc]init];
productPickerView.delegate=self;
productPickerView.tag=2;
txtProduct.inputView=productPickerView;
txtProduct.inputAccessoryView=numberToolbar;
//model pickerview
modelPickerView=[[UIPickerView alloc]init];
modelPickerView.delegate=self;
modelPickerView.tag=3;
txtModel.inputView=modelPickerView;
txtModel.inputAccessoryView=numberToolbar;
}//end of view did load
这是我的picker视图事件:-
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if (pickerView.tag == 1) {
return [devicecatogery count]; //<-----------this is for category picker view
}else if(pickerView.tag == 2)
return [commanProductArray count]; //<-----------this is for product picker view
else
return [commanModelArray count]; //<-------------- this is for model picker view
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (pickerView.tag == 1)
return [devicecatogery objectAtIndex:row];//<----this is for category picker view
else if (pickerView.tag == 2)
return [commanProductArray objectAtIndex:row];//<--------this is for product picker view
else
return [commanModelArray objectAtIndex:row];//<--------- this is for model picker view
}
这个代码对我来说没有意义,我怀疑你的问题在这里:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if ([[categoryArray objectAtIndex:[categoryPickerView selectedRowInComponent:0] ] isEqual:@"Audio"]) {
commanProductArray=[[NSMutableArray alloc]initWithObjects:@"walkman",@"mp3",@"ipod", nil];
if ([[commanProductArray objectAtIndex:[productPickerView selectedRowInComponent:0]] isEqual:@"walkman"]) {
// walkman list display
[productPickerView reloadAllComponents];
选中时,所有pickerView都将进入此回调。首先,您没有检查标记以查看触发的是哪一个pickerView,然后检查所选行是否为音频/视频,如果是第二个/第三个pickers,代码将永远不会通过此操作,因为它们不会选择音频/视频
按照逻辑,如果选择了“音频”,则将commanProductArray
设置为给定值,然后在加载之前立即检查所选值是否等于“walkman”
[productPickerView重新加载所有组件]我怀疑设置了commanProductArray
之后应该直接调用code>,因为您设置了数组并在加载之前检查选择了哪个值
这个问题可以通过简单地放置2或3个NSlog来发现,看看它是否进入了循环
编辑:
您的代码应该遵循以下内容:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// If first picker
if(pickerView.tag == 1)
{
// If Audio
if(row == 0)
{
// Load result for audio
}
// If Video
else if (row == 1)
{
// load result for video
}
}
// If second picker
else if(pickerView.tag == 2)
{
// if Walkman
if(row == 0)
{
// load result for walkman
}
// etc etc etc
}
else if (pickerView.tag == 3)
{
}
}
试试这个:
- (void)viewDidLoad
{
devicecatogery=[[NSArray alloc]initWithObjects:@"Audio",@"Video", nil];
commanProductArray=[[NSMutableArray alloc]initWithObjects:@"walkman",@"mp3",@"ipod", nil];
commanModelArray=[[NSMutableArray alloc]initWithObjects:@"walkman1",@"walkman2",@"walkman3",@"walkman4", nil];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
// tell the picker how many rows are available for a given component
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (pickerView.tag==1) {
return devicecatogery.count;
}
else if(pickerView.tag==2){
return commanProductArray.count;
}
else if (pickerView.tag==3)
{
return commanModelArray.count;
}
return 0;
}
// tell the picker how many components it will have
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
// tell the picker the title for a given component
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *title;
if (pickerView.tag==1)
{
title = [NSString stringWithFormat:@"%@",[devicecatogery objectAtIndex:row]];
}
else if (pickerView.tag==2)
{
if([categoryPickerView selectedRowInComponent:0]==0)
{
commanProductArray=[[NSMutableArray alloc]initWithObjects:@"walkman",@"mp3",@"ipod", nil];
}
else{
commanProductArray=[[NSMutableArray alloc]initWithObjects:@"TV",@"Moniter",@"Projector", nil];
}
title = [NSString stringWithFormat:@"%@",[commanProductArray objectAtIndex:row]];
}
else if (pickerView.tag==3)
{
if ([[devicecatogery objectAtIndex:[categoryPickerView selectedRowInComponent:0]] isEqualToString:@"Audio"]) {
if([productPickerview selectedRowInComponent:0]==0)
{
commanModelArray=[[NSMutableArray alloc]initWithObjects:@"walkman1",@"walkman2",@"walkman3",@"walkman4", nil];
}
else if([productPickerview selectedRowInComponent:0]==1)
{
commanModelArray=[[NSMutableArray alloc]initWithObjects:@"mp31",@"mp32",@"mp33",@"mp34", nil];
}
else if([productPickerview selectedRowInComponent:0]==2)
{
commanModelArray=[[NSMutableArray alloc]initWithObjects:@"ipod1",@"ipod2",@"ipod3",@"ipod4", nil];
}
}
else
{
if([productPickerview selectedRowInComponent:0]==0)
{
commanModelArray=[[NSMutableArray alloc]initWithObjects:@"TV1",@"TV2",@"TV3",@"TV4", nil];
}
else if([productPickerview selectedRowInComponent:0]==1)
{
commanModelArray=[[NSMutableArray alloc]initWithObjects:@"Moniter1",@"Moniter2",@"Moniter3",@"Moniter4", nil];
}
else if([productPickerview selectedRowInComponent:0]==2)
{
commanModelArray=[[NSMutableArray alloc]initWithObjects:@"Projector1",@"Projector2",@"Projector3",@"Projector4", nil];
}
}
title = [NSString stringWithFormat:@"%@",[commanModelArray objectAtIndex:row]];
}
return title;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if (pickerView.tag==1)
{
[productPickerview reloadAllComponents];
[modelPickerview reloadAllComponents];
}
if (pickerView.tag==2) {
[modelPickerview reloadAllComponents];
}
// Handle the selection
}
有关更多详细信息,请查看:
愿它对你有帮助
快乐编码…:)请描述“未相应加载”,显示任何内容,控件正在显示但没有数据,回调未被调用等。其他选择器视图未加载(productPickerview和modelPickerview)我的代码是否正确请帮助@Dhaval Bhadaniath categoryPickerView正在显示音频和视频,但当选择音频时,productPickerView不会显示walkman、mp3、ipod@SimonMcLoughlin@user2598751请发布更多的代码,我不明白你是如何根据代码/评论来实现这一点的。我们需要查看所有声明的变量和所有回调。我已经编辑了u@Simon McLoughlini的代码。我尝试使用NSLog,但它没有在“if”([[categoryArray objectAtIndex:[categoryPickerView SelectedRowIncomonent:0]]isEqual:@“Audio”]{NSLog(@“1”)之后进入循环“西蒙McLoughlin@user2598751…因此,您发现NSlog没有打印,这意味着您的if语句不起作用,这意味着您发现了问题,但在问题中没有提到这一点为什么?为什么您甚至需要进行字符串比较,为什么不检查索引:if(pickerView.tag==1&&row==0)//第0行将等于audio@user2598751刚刚注意到:'isEqual:@“Audio”]'没有进行字符串比较,它应该是'IseQualString:@“Audio”]“我是根据的,但现在它不会进入第三个循环,即加载modelpickerview。它不会加载model@Simon的数组McLoughlin@user2598751我在回答中浏览了你的代码,并指出你所做的结构是错误的。我编辑了我的回答,以向你展示它的外观。你需要阅读我的代码注释你没有理解我说的,因为你没有试图理解其中的区别,你只是在寻找一份施舍,让别人给你代码,让它工作,作为证据,你似乎只是复制了我给出的代码,没有注意到它只有一个if语句是固定的
- (void)viewDidLoad
{
devicecatogery=[[NSArray alloc]initWithObjects:@"Audio",@"Video", nil];
commanProductArray=[[NSMutableArray alloc]initWithObjects:@"walkman",@"mp3",@"ipod", nil];
commanModelArray=[[NSMutableArray alloc]initWithObjects:@"walkman1",@"walkman2",@"walkman3",@"walkman4", nil];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
// tell the picker how many rows are available for a given component
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (pickerView.tag==1) {
return devicecatogery.count;
}
else if(pickerView.tag==2){
return commanProductArray.count;
}
else if (pickerView.tag==3)
{
return commanModelArray.count;
}
return 0;
}
// tell the picker how many components it will have
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
// tell the picker the title for a given component
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *title;
if (pickerView.tag==1)
{
title = [NSString stringWithFormat:@"%@",[devicecatogery objectAtIndex:row]];
}
else if (pickerView.tag==2)
{
if([categoryPickerView selectedRowInComponent:0]==0)
{
commanProductArray=[[NSMutableArray alloc]initWithObjects:@"walkman",@"mp3",@"ipod", nil];
}
else{
commanProductArray=[[NSMutableArray alloc]initWithObjects:@"TV",@"Moniter",@"Projector", nil];
}
title = [NSString stringWithFormat:@"%@",[commanProductArray objectAtIndex:row]];
}
else if (pickerView.tag==3)
{
if ([[devicecatogery objectAtIndex:[categoryPickerView selectedRowInComponent:0]] isEqualToString:@"Audio"]) {
if([productPickerview selectedRowInComponent:0]==0)
{
commanModelArray=[[NSMutableArray alloc]initWithObjects:@"walkman1",@"walkman2",@"walkman3",@"walkman4", nil];
}
else if([productPickerview selectedRowInComponent:0]==1)
{
commanModelArray=[[NSMutableArray alloc]initWithObjects:@"mp31",@"mp32",@"mp33",@"mp34", nil];
}
else if([productPickerview selectedRowInComponent:0]==2)
{
commanModelArray=[[NSMutableArray alloc]initWithObjects:@"ipod1",@"ipod2",@"ipod3",@"ipod4", nil];
}
}
else
{
if([productPickerview selectedRowInComponent:0]==0)
{
commanModelArray=[[NSMutableArray alloc]initWithObjects:@"TV1",@"TV2",@"TV3",@"TV4", nil];
}
else if([productPickerview selectedRowInComponent:0]==1)
{
commanModelArray=[[NSMutableArray alloc]initWithObjects:@"Moniter1",@"Moniter2",@"Moniter3",@"Moniter4", nil];
}
else if([productPickerview selectedRowInComponent:0]==2)
{
commanModelArray=[[NSMutableArray alloc]initWithObjects:@"Projector1",@"Projector2",@"Projector3",@"Projector4", nil];
}
}
title = [NSString stringWithFormat:@"%@",[commanModelArray objectAtIndex:row]];
}
return title;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if (pickerView.tag==1)
{
[productPickerview reloadAllComponents];
[modelPickerview reloadAllComponents];
}
if (pickerView.tag==2) {
[modelPickerview reloadAllComponents];
}
// Handle the selection
}