开发者

UIPickerView not returning the correct data

I have a Main view with a landscapeview and a portraitview that gets loaded as subviews of the wrapper view. Both the landscape view and portraitview has UIPickerView objects that are linked to them. In my source I have them all as properties and I also have a wrapper for the landscape and portrait pickers. They are defined as

@property (nonatomic, retain, readonly) IBOutlet UIPickerView *pickerTown;
@property (nonatomic, retain, readonly) IBOutlet UIPickerView *pickerType;
@property (nonatomic, retain, readonly) IBOutlet UIDatePicker *pickerFrom;
@property (nonatomic, retain, readonly) IBOutlet UIDatePicker *pickerTo;

@property (nonatomic, retain) IBOutlet UIPickerView *landscapePickerTown;
@property (nonatomic, retain) IBOutlet UIPickerView *landscapePickerType;
@property (nonatomic, retain) IBOutlet UIDatePicker *landscapePickerFrom;
@property (nonatomic, retain) IBOutlet UIDatePicker *landscapePickerTo;

@property (nonatomic, retain) IBOutlet UIPickerView *portraitPickerTown;
@property (nonatomic, retain) IBOutlet UIPickerView *portraitPickerType;
@property (nonatomic, retain) IBOutlet UIDatePicker *portraitPickerFrom;
@property (nonatomic, retain) IBOutlet UIDatePicker *portraitPickerTo;

And for each of the readonly wrappers I have

- (UIPickerView *) pickerTown
{
    if(self.landscapeView.superview)
    {
        return landscapePickerTown;
    }else
    {
        return portraitPickerTown;
    }
}

But when I am in portrait or landscape, and I try for instance to say

myHelper.selectedTown = [myHelper.towns objectAtIndex:[pickerTown selectedRowInComponent:0]];

I don't get the selected value but just the first value in the array.

The data gets added as follows:

if(pickerView == landscapePickerType || pickerView == portraitPickerType) {
    return [myHelper.types objectAtIndex:row];
}else if(pickerView == landscapePickerTown || pickerView == portraitPickerTown){
    return [myHelper.towns objectAtIndex:row]; 
}else
{
    return @"";
}

and the array:

myHelper = [Helper sharedManager];
[myHelper.types addObject:@"None"];
[myHelper.types addObject:@"Food & Wine"];
[myHelper.types addObject:@"Test"];
[myHelper.towns addObject:@"None"];
[myHelper.towns addObject:@"Vanderbijlpark"];
[myHelper.towns addObject:@"开发者_如何学PythonTest"];

What am I doing wrong here?


If you are getting only the first element, it is indicative that [pickerTown selectedRowInComponent:0] is evaluating to zero always which will happen only if pickerTown is nil. That again is most likely due to outlet not being connected.

But in this case I think you are accessing the instance variable directly and not the value the method returns. If you are using @synthesize for pickerTown, make that @dynamic. And change the statement to [self.pickerTown selectedRowInComponent:0] to access the value of the getter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜