3

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

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

我还能够获得一些好的代码来做到这一点,并且它在我的 Iphone 设备上运行良好。但是,我发现 Ipad 设备有困难。操作表未显示为完整视图。请看下面的截图。这就是结果!!!

在 Ipad 中带有 Don 按钮的 UIPickerView

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

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 下载了一个出色的示例应用程序

下载后,我已将仅对我来说必须的课程复制到我的应用程序中。

他们用来通过 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];

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

Action-sheet 将 origin 作为传递的对象。

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

如果我将 mySegment 作为原点参数,则操作表原点箭头将从我的段控件中间显示。而不是从选定的选项卡中显示,这太糟糕了,会给我有价值的用户带来困惑。

所以我在段部分下添加了单独的标签,并为上述方法的原始参数提供了它,我解决了我的问题。

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

我可以知道有什么简单的方法吗?

Apple 是否支持 Ipad 中的 ActionSheet+UIPickerView+DoneButton

对此问题的任何帮助表示赞赏

4

4 回答 4

4

在此处输入图像描述

-(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;
}
于 2012-08-03T18:10:01.743 回答
2

在此处输入图像描述

使用 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];//
    }
于 2012-06-22T04:37:00.727 回答
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,在你初始化arrayin后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];

}
于 2012-06-22T07:44:50.673 回答
1

在 iPad 中尝试下面的 UIPicker View 代码

-(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;
}  
}
于 2012-06-22T04:41:31.967 回答