UIPickerView在ActionSheet上的奇怪行为

时间:2012-08-05 09:59:16

标签: objective-c ios uipickerview uitoolbar uiactionsheet

- (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但没有任何成功。什么似乎是问题?

非常感谢。

dismissing pickerview

1 个答案:

答案 0 :(得分:3)

请勿在{{1​​}}内插入UIPickerView,否则您将无法以这种方式获得正确的行为。我的解决方案:

  1. 子类UIActionSheet,实施方法UIButton,返回canBecomeFirstResponder:
  2. 在您的子类YES中实施inputView方法,返回UIButton
  3. (可选)在子类按钮中实现UIPickerView方法,以返回选择器上方的工具栏。
  4. 点按时按下inputAccessoryView按钮。
  5. Voila,当你按下按钮时,你的选择器会像键盘一样显示模式。

    修改

    MyButton.h

    becomeFirstResponder:

    MyButton.m

    #import <UIKit/UIKit.h>
    
    @interface MyButton : UIButton
    
    @end