我在视图中使用两个UIPickers。 第一个选择器有3个组件,第二个有一个组件。 但是当我选择项目时,它显示来自第一个选择器的正确项目,但总是从第二个选择器返回第一个项目而不管选择的行。 请帮忙。
这是我正在使用的代码。
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
if (pickerView == triplePicker)
return 3;
else {
return 1;
}
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (pickerView == triplePicker) {
if (component == kColorComponent)
return[colorList count];
if (component == kClarityComponent)
return[clarityList count];
return[shapeList count];
}
else{
return [listPickerItems count];
}
}
-(NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
if (pickerView == triplePicker) {
if (component == kColorComponent)
return [colorList objectAtIndex:row];
if (component == kClarityComponent)
return [clarityList objectAtIndex:row];
return [shapeList objectAtIndex:row];
}
else{
return [listPickerItems objectAtIndex:row];
}
}
在按下按钮的事件中,我跟随第二个选择器返回所选项目:
NSInteger pickrow = [listPicker selectedRowInComponent:0];
NSString *picked = [listPickerItems objectAtIndex:pickrow];
答案 0 :(得分:0)
我肯定建议使用两个单独的代表来处理两个选择器。
除此之外,我猜你正在使用Interface Builder来设置视图。如果是这种情况,请检查您是否已将listPicker与文件所有者正确链接。如果listPicker为nil,那么selectedRowInComponent:将始终返回null(NSInteger为0),因此总是选择数组中的第一项。
编辑:单独代表的一些示例代码:
您需要创建第二个类作为您的委托,如下所示:
FirstPickerDelegate.h
#import <Foundation/Foundation.h>
@class UntitledViewController;
@interface FirstPickerViewDelegate : NSObject <UIPickerViewDelegate, UIPickerViewDataSource> {
NSArray* values;
IBOutlet UntitledViewController* viewController;
}
-(void) loadData;
@property (nonatomic, retain) NSArray* values;
@property (nonatomic, assign) IBOutlet UntitledViewController* viewController;
@end
FirstPickerViewDelegate.m
#import "FirstPickerViewDelegate.h"
@implementation FirstPickerViewDelegate
@synthesize values;
@synthesize viewController;
-(id) init
{
if ( self = [super init] )
{
[self loadData];
}
return self;
}
-(void) loadData
{
NSArray* array = [[NSArray alloc] initWithObjects:@"first", @"second", @"third", nil];
self.values = array;
[array release];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [values count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [values objectAtIndex:row];
}
-(void) dealloc
{
self.values = nil;
[super dealloc];
}
@end
你的ViewController(这里是UntitledViewController,抱歉名字):
#import <UIKit/UIKit.h>
@class FirstPickerViewDelegate;
@interface UntitledViewController : UIViewController {
IBOutlet UIPickerView* firstPicker;
IBOutlet FirstPickerViewDelegate* firstDelegate;
}
@property (nonatomic, retain) IBOutlet UIPickerView* firstPicker;
@property (nonatomic, retain) IBOutlet FirstPickerViewDelegate* firstDelegate;
@end
基本上你需要在对象列表上删除一个NSObject,并将它的类更改为FirstPickerViewDelegate,然后进行如下连接:
我觉得这次我已经过度训练了,但今天心情很好,所以无论如何:P
关于主要问题:在按下按钮时仔细检查listPicker是否为零,如果不是,请尝试使用
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
追踪错误。