我試圖實現一個彈出式UIPickerView來模擬在Safari中單擊下拉字段時的行爲,迄今爲止有限的成功。我發現:彈出UIPickerView不顯示
,並試圖遵循這一點。到目前爲止,我有我的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
非常感謝您的建議,但我試圖讓您的UIPickerView從屏幕底部向上滑動,就像在Safari中碰到下拉字段時一樣。這似乎導致在它的UITableView的模態對話框。 – rwbutler 2012-03-19 13:41:27