简体   繁体   中英

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. I found:

Bottom pop-up 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).

PopUpPickerViewController has a nib file so that when a button is clicked, the 'change' selector is invoked and the UIPickerView should pop up. At the moment, only the background color of the PopUpPickerView pops up.

Any help you could provide would be very much appreciated. Thanks in advance!

My code is as follows:

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

It turned out that the answer was to do with the coordinates at which the UIPickerView was placed. The UIPickerView was being placed at the bottom of the PopUpPickerView which meant that it was offscreen. 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. Take a look and hope it helps in your project.

Git Repo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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