UIPickerView 在 ActionSheet 上的奇怪行为

Posted

技术标签:

【中文标题】UIPickerView 在 ActionSheet 上的奇怪行为【英文标题】:odd behavior of UIPickerView on ActionSheet 【发布时间】:2012-08-05 09:59:15 【问题描述】:
- (void) actionsheetstart

    actionSheet=[[UIActionSheet alloc] initWithTitle:@"" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
    [actionSheet showInView:self.view];
    UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 480, 32)];
    [pickerToolbar sizeToFit];
    pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
    NSMutableArray *barItems = [[NSMutableArray alloc] init];

    UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonSystemItemCancel target:self action:@selector(cancel_clicked:)];
    [barItems addObject:cancelBtn];
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    [barItems addObject:flexSpace];
    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(done_clicked:)];
    [barItems addObject:doneBtn];
    [pickerToolbar setItems:barItems animated:YES];
    [actionSheet addSubview:pickerToolbar];

    UIPickerView *picker = [[UIPickerView alloc] init];
    picker.frame = CGRectMake(0, 44, 320, 216);
    picker.delegate  = self;
    picker.dataSource = self;
    picker.showsSelectionIndicator = YES;
    [actionSheet addSubview:picker];


-(void)done_clicked:(id)sender

    [actionSheet dismissWithClickedButtonIndex:0 animated:YES];

-(void)cancel_clicked:(id)sender

    [actionSheet dismissWithClickedButtonIndex:0 animated:YES];

由于某种原因,操作表很好,但 UIPickerView 和 UIToolBar 变得疯狂。 我尝试将框架设置为 -px 但没有任何成功。似乎是什么问题?

非常感谢。

【问题讨论】:

【参考方案1】:

不要在UIActionSheet 中插入UIPickerView,这样你不会得到正确的行为。我的解决方案:

    子类UIButton,实现方法canBecomeFirstResponder:,返回YES。 在您的子类UIButton 中实现inputView 方法,返回UIPickerView。 (可选)在您的子类按钮中实现inputAccessoryView 方法以返回您的选择器上方的工具栏。 点击时将您的按钮设为becomeFirstResponder:

瞧,当点击按钮时,您的选择器会像键盘一样模态显示。

编辑:

MyButton.h

#import <UIKit/UIKit.h>

@interface MyButton : UIButton

@end

MyButton.m

#import "MyButton.h"

@implementation MyButton 
    UIDatePicker *_pv;


- (BOOL)canBecomeFirstResponder 
    return YES;


#pragma mark - Picker

- (UIView *)inputView 
    // set up your UIPickerView here
    _pv = [[UIDatePicker alloc] init];
    [_pv setDatePickerMode:UIDatePickerModeTime];
    [_pv setMinuteInterval:30];
    return _pv;


- (UIView *)inputAccessoryView 
    // set up your toolbar here
    UIToolbar *tb = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *ok = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pick:)];
    [tb setItems:[NSArray arrayWithObjects:flex, ok, nil]];
    return tb;


- (void)pick:(id)sender 
    // store picker value here before closing inputView
    [self resignFirstResponder];

@end

【讨论】:

我是 Obj-C 的新手,我真的不知道该怎么做。 :( 不过还是谢谢。 抱歉,让您久等了。有关示例实现,请参阅我上面的编辑。

以上是关于UIPickerView 在 ActionSheet 上的奇怪行为的主要内容,如果未能解决你的问题,请参考以下文章

如何在级联中设置 UIPickerView

UIPickerView 和 UITextField 从 UIPickerView 获取值

UIPickerView - 循环数据

UIPickerView 编程示例?

如何在 UIPickerView 中自动换行

尝试使用 UIScrollView 创建自定义水平 UIPickerView - 如何像在 UIPickerView 中一样淡出边缘?