在Ipad中使用完成按钮的UIPickerView

时间:2012-06-22 04:25:31

标签: objective-c iphone ipad uipickerview actionsheetpicker

我遇到过一个问题,要在Ipad中显示带有完成按钮的 UIPickerView。 我通过许多链接和博客进行了详细的研究,并得到了“从 UIActionSheet 显示UIPickerView”的建议

我看到很多帖子都与此相关,但没有好的答案。所以请不要将其作为副本关闭。

此外,我能够获得一些好的代码,并且它在我的Iphone设备中运行良好。 然而,我发现Ipad设备有困难。 操作表未显示为完整视图。 请看下面的截图。这是结果!!!

UIPickerView with a Don button in Ipad

用于执行此操作的代码粘贴在下面。

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                    delegate:nil
                                                    cancelButtonTitle:nil
                                                    destructiveButtonTitle:nil
                                                    otherButtonTitles:nil];

[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

CGRect pickerFrame = CGRectMake(0, 40, 0, 0);

UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;

[actionSheet addSubview:pickerView];
[pickerView release];

UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Close"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:@selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:closeButton];
[closeButton release];

[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];

[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];

然后我从github通过sample pickers

下载了一个优秀的示例应用程序

下载后,我已经将我必须的课程复制到我的申请中。

他们用于通过Action-Sheet显示UIPickerView + Done按钮的方法如下所述

ActionStringDoneBlock done = ^(ActionSheetStringPicker *picker, NSInteger selectedIndex, id selectedValue) {

        if ([myLabel respondsToSelector:@selector(setText:)]) {
            [myLabel performSelector:@selector(setText:) withObject:selectedValue];
        }
    };

    ActionStringCancelBlock cancel = ^(ActionSheetStringPicker *picker) {

        NSLog(@"Block Picker Canceled");
    };

    NSArray *colors = [NSArray arrayWithObjects:@"Red", @"Green", @"Blue", @"Orange", nil];//picker items to select


    [ActionSheetStringPicker showPickerWithTitle:@"Select a Block" rows:colors initialSelection:0 doneBlock:done cancelBlock:cancel origin:myButton];

在最后一行代码中,他们将参数用作 origin:,我们可以将任何对象(按钮,标签等)传递给它。

操作表将原点作为传递的对象。

我的问题又来了:)。我已经使用 段控件 来根据我的条件选择时间。

如果我将mySegment作为原点参数,则动作表原点箭头将显示在我的段控件的中间。不是来自所选的选项卡,这太糟糕了,会让我的宝贵用户感到困惑。

所以我在段节下添加了单个标签,并为上述方法的origin参数赋予它,我修复了我的问题。

但我知道这不是一个好的解决办法:)

我知道有没有简单的方法呢?

Apple支持Ipad中的ActionSheet + UIPickerView + DoneButton 吗?

有关此问题的任何帮助都是感谢

5 个答案:

答案 0 :(得分:4)

enter image description here

-(void)viewDidload
{
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button1.frame = CGRectMake(165,165, 135,35);
    [button1 setTitle:@"Type    @" forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(button1) forControlEvents:UIControlEventTouchUpInside];
    [s addSubview:button1];
}

-(void)button1
{
    items1 =[[NSMutableArray alloc]initWithObjects:@"H",@"E",@"T",@"K",nil];
    myPickerView1 =[[UIPickerView alloc] initWithFrame:CGRectMake(60,80,200,300)];
    myPickerView1.transform = CGAffineTransformMakeScale(0.75f, 0.75f);
    myPickerView1.delegate = self;
    myPickerView1.dataSource = self;
    myPickerView1.showsSelectionIndicator = YES;
    myPickerView1.backgroundColor = [UIColor clearColor];
    myPickerView1.tag=1;
    [myPickerView1 selectRow:1 inComponent:0 animated:YES];
    [self.view addSubview:myPickerView1];
}

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
    switch (pickerView.tag)
    {
    case 1:
        return [items1 count];
            break;
    case 2:
        return [items2 count];
        break;
    }
    return 0;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    switch (pickerView.tag)
    {
    case 1:
        return[items1 objectAtIndex:row];
        break;
    case 2:
        return[items2 objectAtIndex:row];
        break;
    }
        return 0;
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    switch (pickerView.tag)
    {
    case 1:
        {
            [button1 setTitle:[items1 objectAtIndex:row] forState:UIControlStateNormal];
        }
        break;
    case 2:
        {
            [button2 setTitle:[items2 objectAtIndex:row] forState:UIControlStateNormal];
        }break;
    }
    pickerView.hidden = YES;
}

答案 1 :(得分:2)

enter image description here

在选择器中使用UIPopoverController完成按钮,创建一个视图控制器类,在其中选择一个选择器并添加导航取消和完成按钮。

    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:nextViewController];
    _datePickerPopover = [[UIPopoverController alloc] initWithContentViewController:navigationController];
    nextViewController.datePickerPopover = _datePickerPopover;
    _datePickerPopover.delegate=self;
    [_datePickerPopover setPopoverContentSize:CGSizeMake(320, 453) animated:NO];
    if (isSearchOpen) {
        [_datePickerPopover presentPopoverFromRect:CGRectMake(btn.frame.origin.x+10+245, btn.frame.origin.y+100-scrollPointY, 44, 44) inView:self.splitViewController.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];  
    }
    else
    {
        [_datePickerPopover presentPopoverFromRect:CGRectMake(btn.frame.origin.x+10+245, btn.frame.origin.y+55, 44, 44) inView:self.splitViewController.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];//
    }

答案 2 :(得分:2)

您必须使用UIPopOverController

首先,为 iPhone 创建一个UIPickerViewController。你需要它用于笔尖,它将被推入popOver。在ViewWithPicker

中初始化选择器

·H

#import <UIKit/UIKit.h>

@class ViewWithPickerController;

@protocol PopoverPickerDelegate

@required

- (void) viewWithPickerController:(ViewWithPickerController*) viewWithPickerController didSelectValue:(NSString*) value;

@end

@interface ViewWithPickerController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> {
    IBOutlet UIPickerView *pickerView;
    id<PopoverPickerDelegate> delegate;
    NSMutableArray *array;  
}
@property(nonatomic, retain) IBOutlet UIPickerView *pickerView;
@property(nonatomic, assign) id<PopoverPickerDelegate> delegate;
@end

.m,在array中初始化viewDidLoad后,选择器方法:

// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)picker {
    return 1;
}

// returns the number of rows in each component.


- (NSInteger)pickerView:(UIPickerView *)picker numberOfRowsInComponent:(NSInteger)component {
    return [array count];
}

//returns the string value for the current row
- (NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent:(NSInteger)component {
   return [array objectAtIndex:row];
}

//handle selection of a row
- (void)pickerView:(UIPickerView *)picker didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    NSString *value = [pickerView.delegate pickerView:picker titleForRow:row forComponent:component];

    //notify the delegate about selecting a value
    if(delegate != nil)
        [delegate viewWithPickerController:self didSelectValue:value];
}

然后,将viewWithPicker导入到主类中,创建一个按钮并为其执行此操作:

- (IBAction) showPickerPopupAction:(id) sender {
    self.viewWithPickerController = [[[ViewWithPickerController alloc] initWithNibName:@"ViewWithPicker" bundle:[NSBundle mainBundle]] autorelease];


    viewWithPickerController.contentSizeForViewInPopover = 
    CGSizeMake(viewWithPickerController.view.frame.size.width, viewWithPickerController.view.frame.size.height);

    viewWithPickerController.delegate = self;

    self.popoverController = [[[UIPopoverController alloc]
                               initWithContentViewController:viewWithPickerController] autorelease];


    [self.popoverController presentPopoverFromRect:popoverButtonForPicker.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    popoverController.delegate = self;

}

并选择特定值

- (void) viewWithPickerController:(ViewWithPickerController*) viewWithPickerController didSelectValue:(NSString*) value
{   
    yourLabel.text = [NSString stringWithFormat:@"%@ ",value];

}

答案 3 :(得分:1)

在iPad中试用以下UIPickerView代码

-(IBAction)tDriveBtnPressed:(id)sender
{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
txtDate.text = [NSString stringWithFormat:@"%@",
                [df stringFromDate:[NSDate date]]];
[df release];

UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 300, 44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDone:)];
[barItems addObject:doneBtn];
[doneBtn release];
[pickerToolbar setItems:barItems animated:YES];
[barItems release];

datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;

CGRect pickerRect = datePicker.bounds;
datePicker.bounds = pickerRect;

UIViewController* popoverContent = [[UIViewController alloc] init];
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 344)];
popoverView.backgroundColor = [UIColor whiteColor];

datePicker.frame = CGRectMake(0, 44, 320, 300);
[datePicker addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged];
[popoverView addSubview:pickerToolbar];
[popoverView addSubview:datePicker];
popoverContent.view = popoverView;

//resize the popover view shown
//in the current view to the view's size
popoverContent.contentSizeForViewInPopover = CGSizeMake(320, 244);

//create a popover controller
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
CGRect popoverRect = [self.view convertRect:[tDriveBtn frame] 
                                   fromView:[tDriveBtn superview]];

popoverRect.size.width = MIN(popoverRect.size.width, 100) ; 
popoverRect.origin.x  = popoverRect.origin.x; 
// popoverRect.size.height  = ; 

[popoverController 
 presentPopoverFromRect:popoverRect
 inView:self.view 
 permittedArrowDirections:UIPopoverArrowDirectionAny
 animated:YES];


//release the popover content
[popoverView release];
[popoverContent release];

}
-(void)dateChange:(id)sender
{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
txtDate.text= [NSString stringWithFormat:@"%@",
               [df stringFromDate:datePicker.date]];
[df release];
}
- (void)pickerDone:(id)sender
{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
txtDate.text= [NSString stringWithFormat:@"%@",
               [df stringFromDate:datePicker.date]];
[df release];

if (popoverController != nil) {
    [popoverController dismissPopoverAnimated:YES];
    self.popoverController=nil;
}  
}

答案 4 :(得分:0)

项目ActionSheetPicker-3.0允许完全按照您的描述实施。