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

Posted

技术标签:

【中文标题】如何为 iOS 7 创建弹出式 uipicker 和 uitextfield?【英文标题】:how to create a popup uipicker and uitextfield for iOS 7? 【发布时间】:2013-10-24 11:16:07 【问题描述】:

我需要将选择器添加到我的文本框中,但不知道如何让它们工作。我已经阅读了许多教程,向您展示了如何从头开始,如果您有一个现有的应用程序并且您想要添加功能,这有点没用,在大多数情况下它们并不是很有帮助。

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

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

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

还有一件事,这是 ios7,我正在使用 storyboards AND arc

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

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

这给了我一些想法,但不完全是我想要的,尝试了这些建议并最终得到比其他任何东西都多的错误。

谢谢。

【问题讨论】:

【参考方案1】:
 @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];

享受你的选择器;)

【讨论】:

感谢您的回复。我知道在哪里添加委托视图(在 .h 文件中),但是“实现组件数量”?什么组件? -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView numberOfComponentsInPickerView & numberOfRowsInComponent ..pickerView 委托的两个必需函数 太棒了。新手问题,那些应该去哪里?由于我们以编程方式执行此操作,所以我在这里有点迷路。你能再礼貌一点,回到你发布的代码,并在我的 viewcontroller.m 和 .h 文件中添加注释吗?记得 -> 新手...好吗?谢谢。 编辑了我的代码以便更好地理解,......而且我有“1”的声誉,先生,你不是新手:D【参考方案2】:

勾选这个,制作一个全屏 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 置于其他视图的顶部,并使其默认隐藏

【讨论】:

嗯,我正在使用故事板。【参考方案3】:

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

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

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

【讨论】:

以上是关于如何为 iOS 7 创建弹出式 uipicker 和 uitextfield?的主要内容,如果未能解决你的问题,请参考以下文章

如何为每个 wordpress 点击后创建单独的弹出表单

UIPicker 和 UIActionsheet 不再工作 iOS7

如何为表情符号制作有效的弹出视图?

如何为 iOS 7 和 iOS 8 实现搜索控制器

如何为azure虚拟机创建双网卡

Swift:使用 UIPicker 自定义 DatePicker