我已经使用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
答案 0 :(得分:0)
pickerviewLoadedFromXib.delegate = self;
pickerviewLoadedFromXib.dataSource = self;
在获得pickerviewloadedfromxib变量后弹出这两个...