我用 xib 创建了一个自定义选择器视图,它从数组中加载数据并正确显示它。现在我的问题是,这个选择器视图没有滚动。实际上这个选择器视图被加载到另一个视图上。因此,当我单击选择器视图时,单击似乎是在已加载它的基础视图上。下面是代码。谁能告诉我如何纠正这个问题。谢谢
#import <UIKit/UIKit.h>
@class PickerView;
@protocol PickerViewDelegate <NSObject>
-(void) didSelectValueAtIndex;
@end
@interface PickerView : UIView <UIPickerViewDataSource, UIPickerViewDelegate>
@property (strong, nonatomic) IBOutlet UIPickerView *pickerView;
@property (nonatomic,weak) id<PickerViewDelegate> PickerViewDelegate;
-(id) showPickerView : (UIButton *) senderButton dataArray: (NSArray *) dataArray viewframe: (CGRect) viewFrame;
-(void) hidePickerView : (UIButton *) senderButton;
@end
#import "PickerView.h"
@interface PickerView ()
@property (nonatomic,retain) NSArray * pickerData;
@property (nonatomic,retain) UIButton *button;
@end
@implementation PickerView
@synthesize pickerView;
@synthesize PickerViewDelegate;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(id) showPickerView : (UIButton *) senderButton dataArray: (NSArray *) dataArray viewframe:(CGRect)viewFrame{
self.button = senderButton;
self.pickerView = (UIPickerView *)[super init];
if (self) {
//inorder to load xib
NSArray *theView = [[NSBundle mainBundle] loadNibNamed:@"PickerView" owner:self options:nil];
UIView *pickerviewLoadedFromXib = [theView objectAtIndex:0];
CGRect btn = senderButton.frame;
self.pickerData = dataArray;
pickerviewLoadedFromXib.frame = CGRectMake(btn.origin.x , btn.origin.y+ viewFrame.size.height, pickerviewLoadedFromXib.frame.size.width, pickerviewLoadedFromXib.frame.size.height);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
pickerviewLoadedFromXib.frame = CGRectMake(btn.origin.x, btn.origin.y+btn.size.height, btn.size.width, 200);
[UIView commitAnimations];
[senderButton.superview addSubview:self];
[self addSubview:pickerviewLoadedFromXib];
}
return self;
}
-(void) hidePickerView : (UIButton *) senderButton{
NSLog(@"hide picker view");
}
-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return self.pickerData.count;
}
-(NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [self.pickerData objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog(@"selectd value %@", [self.pickerData objectAtIndex:row]);
}
@end