保存视图中的两个 PickerView 问题?

发布于 2024-08-26 03:34:47 字数 1927 浏览 7 评论 0 原文

我试图在同一个视图中有 2 个选择器视图。除了两件事之外,它是有效的。如果一个选择器视图的行数多于另一个选择器视图的行数,则当从具有更多项目的选择器视图中选择项目时,应用程序会崩溃。另外,我为 pickerviews 创建了一个 NSLog,控制台显示我正在一次选择两个项目,而实际上我只处理一个 pickerview。我知道这听起来有点令人困惑,但我包含了所有代码。先感谢您。

list 和 list2 是 NSMutableArrays

列表有 4 项 list2 有 5 项

存在错误:

* 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“* -[NSCFArray objectAtIndex:]:索引 (4) 超出界限 (4)”

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView{
    if([thePickerView isEqual:pickerView1 ]){       
        return 1;
    }
    else if([thePickerView isEqual:pickerView2]){ 

        return  1;
    }

    else{
        return 0;
    }
}

-(NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component{

    if([thePickerView isEqual:pickerView1 ]){ 
        return [list count];
    }
    else if([thePickerView isEqual:pickerView2]){       
        return [list2 count];
    }

    else{
        return 0;
    }
}

-(NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{

    if([thePickerView isEqual:pickerView1 ]){ 
        return [list objectAtIndex:row];
    }
    else if([thePickerView isEqual:pickerView2]){       
        return [list2 objectAtIndex:row];
    }

    else{
        return 0;
    }

} 

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

    NSLog(@"Selected item %@. Index of selected item:%i", [list objectAtIndex:row], row);
    NSLog(@"Selected item %@. Index of selected item:%i", [list2 objectAtIndex:row], row);
    NSLog(@"Selected item %@. Index of selected item:%i", [list3 objectAtIndex:row], row);

if([thePickerView isEqual:pickerView1 ]){
//Do Something
}
else if([thePickerView isEqual:pickerView2 ]){
//Do Something
}
else if([thePickerView isEqual:pickerView3 ]){
//Do Something
}
}

I'm trying to have 2 pickerviews in the same view. It works except for two things. If one pickerview has more rows than the other the app crashes when picking an item from the pickerview with more items. Also I created an NSLog for the pickerviews and the console shows that I'm picking two items at once when in fact i'm only dealing with one pickerview. I know it sounds a bit confusing but I'm including all the code. Thank you in advance.

list and list2 are NSMutableArrays

list has 4 items
list2 has 5 items

There error:

* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[NSCFArray objectAtIndex:]: index (4) beyond bounds (4)'

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView{
    if([thePickerView isEqual:pickerView1 ]){       
        return 1;
    }
    else if([thePickerView isEqual:pickerView2]){ 

        return  1;
    }

    else{
        return 0;
    }
}

-(NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component{

    if([thePickerView isEqual:pickerView1 ]){ 
        return [list count];
    }
    else if([thePickerView isEqual:pickerView2]){       
        return [list2 count];
    }

    else{
        return 0;
    }
}

-(NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{

    if([thePickerView isEqual:pickerView1 ]){ 
        return [list objectAtIndex:row];
    }
    else if([thePickerView isEqual:pickerView2]){       
        return [list2 objectAtIndex:row];
    }

    else{
        return 0;
    }

} 

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

    NSLog(@"Selected item %@. Index of selected item:%i", [list objectAtIndex:row], row);
    NSLog(@"Selected item %@. Index of selected item:%i", [list2 objectAtIndex:row], row);
    NSLog(@"Selected item %@. Index of selected item:%i", [list3 objectAtIndex:row], row);

if([thePickerView isEqual:pickerView1 ]){
//Do Something
}
else if([thePickerView isEqual:pickerView2 ]){
//Do Something
}
else if([thePickerView isEqual:pickerView3 ]){
//Do Something
}
}

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

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

发布评论

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

评论(1

戈亓 2024-09-02 03:34:47

您的问题在这里:

NSLog(@"Selected item %@. Index of selected item:%i", [list objectAtIndex:row], row);
NSLog(@"Selected item %@. Index of selected item:%i", [list2 objectAtIndex:row], row);
NSLog(@"Selected item %@. Index of selected item:%i", [list3 objectAtIndex:row], row);

发生的情况是上面的列表之一被发送一条消息以返回超出其范围的对象。在向日志发送消息之前,您应该检查调用来自哪个选择器:

if([thePickerView isEqual:pickerView1 ]){
  NSLog(@"Selected item %@. Index of selected item:%i", [list objectAtIndex:row], row);
  //Do Something
}
else if([thePickerView isEqual:pickerView2 ]){
  NSLog(@"Selected item %@. Index of selected item:%i", [list2 objectAtIndex:row], row);
  //Do Something
}
else if([thePickerView isEqual:pickerView3 ]){
  NSLog(@"Selected item %@. Index of selected item:%i", [list3 objectAtIndex:row], row);
  //Do Something
}

Your problem is here:

NSLog(@"Selected item %@. Index of selected item:%i", [list objectAtIndex:row], row);
NSLog(@"Selected item %@. Index of selected item:%i", [list2 objectAtIndex:row], row);
NSLog(@"Selected item %@. Index of selected item:%i", [list3 objectAtIndex:row], row);

What happens is that one of the above lists is sent a message to return an object which is out of its bounds. You should check what picker the call came from before sending message to the log:

if([thePickerView isEqual:pickerView1 ]){
  NSLog(@"Selected item %@. Index of selected item:%i", [list objectAtIndex:row], row);
  //Do Something
}
else if([thePickerView isEqual:pickerView2 ]){
  NSLog(@"Selected item %@. Index of selected item:%i", [list2 objectAtIndex:row], row);
  //Do Something
}
else if([thePickerView isEqual:pickerView3 ]){
  NSLog(@"Selected item %@. Index of selected item:%i", [list3 objectAtIndex:row], row);
  //Do Something
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文