如何为iOS 7创建弹出uipicker和uitextfield?

时间:2013-10-24 11:16:07

标签: ios popup uitextfield uipickerview uipickerviewcontroller

我需要在我的文本框中添加选择器,但无法弄清楚如何让它们工作。我已经阅读了一些教程,向您展示如何从头开始,如果您有一个现有的应用程序,并且您想要添加功能,那么它们是无用的,因为大多数情况下它们都不是很有帮助。

我正在寻找与下图非常相似的内容。下面显示的那个显然是 UIDatepicker ,但理想情况下

  

我正在寻找“地方”,“期限”和“门”的自定义选择器。

所以现在,跳过UIDatepicker项目,专注于自定义选择器项目。

  

还有一件事,这是iOS7,我正在使用故事板 arc

popup pickerview

我遇到了这个(http://www.youtube.com/watch?v=BtmQp5aP304)这真的很有帮助,但它又是从头开始的。我无法弄清楚的是如何在我现有的代码中实现它。所以我正在寻找一个演练,一个有人可以指点我的教程让我开始。

  

stackoverflow上有这个 - >   how to pop up datePicker when text filed click and disappear when editing done

这给了我一些想法,但不完全是我正在寻找的东西,尝试了这些建议,结果发现了更多的错误。

感谢。

3 个答案:

答案 0 :(得分:4)

 @interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>

@implementation ViewController
{
  UIPopoverController *popoverController;
      NSString *currentPick;
    ….
}
.
.
.
-(void)makePicker{

 UIPickerView *picker = [[[UIPickerView alloc]init]autorelease];
        picker.frame = CGRectMake(0, 50, 320, 1000);
        picker.showsSelectionIndicator = YES;
        picker.delegate = self;
 UIButton *done = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [done setBackgroundImage:[UIImage imageNamed:@"Save.png"] forState:UIControlStateNormal];
        [done setFrame:CGRectMake(260, 10, 50, 30)];
        [done addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
 UIButton *cancel = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [cancel setBackgroundImage:[UIImage imageNamed:@"Cancel.png"] forState:UIControlStateNormal];
        [cancel setFrame:CGRectMake(10, 10, 50, 30)];
        [cancel addTarget:self action:@selector(cancelButton:) forControlEvents:UIControlEventTouchUpInside];


 if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone)
    {
        UIActionSheet *pickerAction = [[[UIActionSheet alloc]initWithTitle:[cells objectAtIndex:indexPath.section] delegate:self cancelButtonTitle:nil destructiveButtonTitle:Nil otherButtonTitles:nil]autorelease];
                    [pickerAction addSubview:picker];
        [pickerAction addSubview:done];
        [pickerAction addSubview:cancel];
        [pickerAction showInView:self.view];
        pickerAction.frame = CGRectMake(0,[UIScreen mainScreen].bounds.size.height - 330,320, 350);
    }else
    {
        UIViewController* popoverContent = [[UIViewController alloc] init];
        UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
        [popoverView addSubview:picker];
        [popoverView addSubview:done];
        [popoverView addSubview:cancel];
        [popoverContent.view addSubview:popoverView];
        popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
        [popoverController presentPopoverFromRect:<your frame> inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
        popoverContent.contentSizeForViewInPopover = CGSizeMake(320, 260);
        [popoverView release];
        [popoverContent release];

    }
}
.
.
.

// mainArray here is a NSArray of several arrays, these arrays contain the titles for different selections… e.g. @[fontArray,colourArray,nameArray]

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

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

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
 return [[mainArray objectAtIndex:clickedAt] objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
   currentPick = [[mainArray objectAtIndex:clickedAt] objectAtIndex:row];
}


- (void)doneButton:(UIButton *)button
{
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone)
{
    UIActionSheet *as = (UIActionSheet *)[button superview];
    [as dismissWithClickedButtonIndex:0 animated:YES];
}
else
    [popoverController dismissPopoverAnimated:YES];

// save your selections here using currentPick object
}

- (void)cancelButton:(UIButton *)button
{
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone)
{
    UIActionSheet *as = (UIActionSheet *)[button superview];
    [as dismissWithClickedButtonIndex:0 animated:YES];
}
else
    [popoverController dismissPopoverAnimated:YES];
}

享受你的选择;)

答案 1 :(得分:1)

选中此项,制作全屏 UIView UIImageView UIPickerView 和两个 UIButtons (<然后在.xib中强>保存和取消 在你的.h文件中建立他们的出口:

@property (retain, nonatomic) IBOutlet UIView *bg_view;
@property (retain, nonatomic) IBOutlet UIImageView *bg_customImageView;
@property (retain, nonatomic) IBOutlet UIPickerView *myPicker;
- (IBAction)cancelOut:(UIButton *)sender;
- (IBAction)saveOut:(UIButton *)sender;
@property (retain, nonatomic) IBOutlet UIButton *saveButton;
@property (retain, nonatomic) IBOutlet UIButton *cancelButton;

然后在.m文件中:

-(void)customPickerShow
{
[self.bg_view setBackgroundColor:[UIColor colorWithRed:0.3 green:0.25 blue:0.05 alpha:0.5]];
[self.bg_view setHidden:NO];
self.navigationController.toolbarHidden = YES;
self.navigationController.navigationBarHidden = YES;
[self showMyPicker];
}

-(void)showMyPicker
{
[self.myPicker reloadAllComponents];
[self animatePicker];
}

-(void)animatePicker
{
self.cancelButton.frame = CGRectMake((framewidth/2) - 130, frameheight, 70, 1);
self.saveButton.frame = CGRectMake((framewidth/2) + 60, frameheight, 70, 1);
self.bg_customImageView.frame = CGRectMake((framewidth/2) - 140, frameheight, 280, 1);
[UIView beginAnimations:Nil context:Nil];
[UIView setAnimationDuration:0.3];
[UIView setAnimationDelegate:self];
self.bg_customImageView.frame = CGRectMake((framewidth/2) - 150, frameheight - 230, 300, 300);
self.cancelButton.frame = CGRectMake((framewidth/2) - 130, frameheight - 220, 50, 50);
self.saveButton.frame = CGRectMake((framewidth/2) + 80, frameheight - 220, 50, 50);
[UIView commitAnimations];
}


- (IBAction)cancelOut:(UIButton *)sender {
self.navigationController.navigationBarHidden = NO;
self.navigationController.toolbarHidden = NO;
[self.bg_view setHidden:YES];
}

- (IBAction)saveOut:(UIButton *)sender {

// save here your selections

self.navigationController.navigationBarHidden = NO;
self.navigationController.toolbarHidden = NO;
[self.bg_view setHidden:YES];
}

调用此 [self customPickerShow]; 函数使其正常工作..

这看起来像带有选择器和按钮的操作表..您可以选择背景图像以及您在animatePicker功能中选择框架

进行自定义

请勿忘记将此bg_view置于其他视图的顶部并默认设置为

答案 2 :(得分:0)

纯粹使用UIStoryboard可能无法解决项目,也不能解决最佳方式。这是我的解决方案。 1)创建一个CustomPicker 2)定义您的数据源和代理 3)将CustomPicker附加到目标TextField的inputView。 4)您可以在委托方法中进行设置      - (Bool)textFieldShouldBeginEditing:(UITextField *)textField

在这种方法中,我们通过编码来添加Picker(而不是通过UIStoryboard)。

我有this demo project来表明这个目的。