简体   繁体   English

弹出的UIPickerView不显示

[英]Pop Up UIPickerView Not Displayed

I'm attempting to implement a pop-up UIPickerView to emulate the behavior when clicking on drop-down field within Safari with limited success so far. 我正在尝试实现一个弹出的UIPickerView来模拟当您单击Safari中的下拉字段时的行为,但到目前为止效果有限。 I found: 我发现:

Bottom pop-up UIPicker? 底部弹出式UIPicker?

and have attempted to follow this. 并试图遵循此。 So far, I have my PopUpPickerView showing, but the UIPickerView itself does not (I just see the view background color showing). 到目前为止,我已经显示了PopUpPickerView,但是UIPickerView本身还没有显示(我只是看到显示的视图背景色)。

PopUpPickerViewController has a nib file so that when a button is clicked, the 'change' selector is invoked and the UIPickerView should pop up. PopUpPickerViewController具有一个nib文件,因此,当单击按钮时,将调用“更改”选择器,并应弹出UIPickerView。 At the moment, only the background color of the PopUpPickerView pops up. 此刻,仅弹出PopUpPickerView的背景色。

Any help you could provide would be very much appreciated. 您能提供的任何帮助将不胜感激。 Thanks in advance! 提前致谢!

My code is as follows: 我的代码如下:

PopUpPickerViewController.h 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 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 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 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

It turned out that the answer was to do with the coordinates at which the UIPickerView was placed. 原来,答案与放置UIPickerView的坐标有关。 The UIPickerView was being placed at the bottom of the PopUpPickerView which meant that it was offscreen. UIPickerView被放置在PopUpPickerView的底部,这意味着它不在屏幕上。 After adjusting the coordinates, it all worked out fine. 调整坐标后,一切正常。

I have used SBTableAlert several times and it seems to provide what you require but with a UITableView instead of a UIPickerView. 我已经使用了SBTableAlert几次,它似乎可以提供您所需要的内容,但是提供了UITableView而不是UIPickerView。 Take a look and hope it helps in your project. 看一看,希望对您的项目有所帮助。

Git Repo Git回购

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM