Я пытаюсь реализовать всплывающее окно UIPickerView, чтобы имитировать поведение при щелчке по раскрывающемуся полю в Safari с ограниченным успехом.Я нашел:
Нижнее всплывающее окно UIPicker?
и попытался выполнить это.Пока у меня есть мой PopUpPickerView, но сам UIPickerView нет (я просто вижу цвет фона представления).
PopUpPickerViewController имеет файл пера, так что при нажатии кнопки селектор «изменения»вызывается, и должен появиться 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
PopUpPickerView.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