2012-03-19 74 views
0

我试图实现一个弹出式UIPickerView来模拟在Safari中单击下拉字段时的行为,迄今为止有限的成功。我发现:弹出UIPickerView不显示

Bottom pop-up UIPicker?

,并试图遵循这一点。到目前为止,我有我的PopUpPickerView显示,但UIPickerView本身不(我只看到视图背景颜色显示)。

PopUpPickerViewController有一个nib文件,所以当单击一个按钮时,'change'选择器被调用,并且UIPickerView应该弹出。此刻,只有PopUpPickerView的背景颜色弹出。

任何帮助你可以提供将非常感激。提前致谢!

我的代码如下:

PopUpPickerViewController.h

#import <UIKit/UIKit.h> 
#import <Foundation/Foundation.h> 
#import "PopUpPickerView.h" 

#define UIColorMakeRGBA(nRed, nGreen, nBlue, nAlpha) [UIColor colorWithRed: (nRed)/255.0f green: (nGreen)/255.0f blue:(nBlue)/255.0f alpha:nAlpha] 

@class PopUpPickerView; 

@interface PopUpPickerViewController : UIViewController{ 
    NSArray *pickerData; 
    PopUpPickerView *pickerView; 
    UIPickerView *picker; 
} 

-(IBAction)change:(id)sender; 
-(void)addSubviewToWindow:(UIView*) addView; 

@property(nonatomic, retain) PopUpPickerView *pickerView; 
@property(nonatomic, retain) NSArray *pickerData; 
@property(nonatomic, retain) UIPickerView *picker; 

@end 

PopUpPickerViewController.m

#import "PopUpPickerViewController.h" 
#import "PopUpPickerView.h" 

@implementation PopUpPickerViewController 

@synthesize pickerData, pickerView, picker; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.pickerView = [[PopUpPickerView alloc] initWithFrame:CGRectMake(0.0, 174.0, 320.0, 286.0)]; 
    self.picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 70.0, 320.0, 216.0)]; 
    self.pickerView.picker = self.picker; 
    //[self.picker addTarget:self action:@selector(pickerValueChanged:) forControlEvents:UIControlEventValueChanged]; 
    self.pickerView.parentViewController = self; 
    [self.pickerView addSubview:self.picker]; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

-(IBAction)change:(id)sender{ 
    PopUpPickerView *pView = (PopUpPickerView*) pickerView; 
    [pView animateDatePicker:YES]; 

} 

-(void)addSubviewToWindow:(UIView*) addView{ 
    [self.view addSubview:addView]; 
} 

@end 

PopUpPickerView .H

#import <UIKit/UIKit.h> 
#import "PopUpPickerViewController.h" 

@class PopUpPickerViewController; 

@interface PopUpPickerView : UIView<UIPickerViewDelegate, UIPickerViewDataSource>{ 
    UIPickerView *picker; 
    PopUpPickerViewController *parentViewController; 
    NSArray *pickerData; 
} 

-(void)animateDatePicker:(BOOL)show; 

@property(nonatomic, retain) UIPickerView *picker; 
@property(nonatomic, retain) NSArray *pickerData; 
@property(nonatomic, retain) PopUpPickerViewController *parentViewController; 

@end 

PopUpPicker View.m

#import "PopUpPickerView.h" 
#import "PopUpPickerViewController.h" 

@implementation PopUpPickerView 

@synthesize picker, pickerData, parentViewController; 

- (void)animateDatePicker:(BOOL)show { 

    pickerData = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", nil]; 

    self.picker.delegate = self; 
    self.picker.dataSource = self; 

    CGRect screenRect = self.frame; 
    CGSize pickerSize = [self.picker sizeThatFits:CGSizeZero]; 

    CGRect startRect = CGRectMake(0.0, 
           screenRect.origin.y + screenRect.size.height, 
           pickerSize.width, pickerSize.height); 

    CGRect pickerRect = CGRectMake(0.0, 
           screenRect.origin.y + screenRect.size.height, 
           pickerSize.width, 
           pickerSize.height); 

    self.picker.frame = pickerRect; 
    self.backgroundColor = UIColorMakeRGBA(255, 125, 64, 0.7f - (int)show * 0.7f); 

    if (show) { 
     self.picker.frame = startRect; 
     PopUpPickerViewController *controller = (PopUpPickerViewController*) self.parentViewController; 
     [controller addSubviewToWindow:self]; 
    } 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.3f]; 
    [UIView setAnimationDelegate:self]; 

    self.backgroundColor = UIColorMakeRGBA(255, 125, 64, 0.0f + (int)show * 0.7f); 

    if (show) { 
     self.picker.frame = pickerRect; 

    } else { 
     [UIView setAnimationDidStopSelector:@selector(slideDownDidStop)]; 
     self.picker.frame = startRect; 
    } 

    [UIView commitAnimations]; 
} 

-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView 
{ 
    return 1; 
} 

-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{ 
    return [pickerData count]; 
} 

-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
    return [self.pickerData objectAtIndex:row]; 
} 


@end 

回答

1

原来答案是与该UIPickerView放在坐标做。 UIPickerView被放置在PopUpPickerView的底部,这意味着它在屏幕外。调整坐标后,一切正常。

0

我已经使用SBTableAlert几次,它似乎提供您所需要的,但有一个UITableView,而不是一个UIPickerView。看一看,并希望它有助于您的项目。

Git Repo

+0

非常感谢您的建议,但我试图让您的UIPickerView从屏幕底部向上滑动,就像在Safari中碰到下拉字段时一样。这似乎导致在它的UITableView的模态对话框。 – rwbutler 2012-03-19 13:41:27