0
- (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 に設定しようとしましたが、成功しませんでした。問題に見えるのは?

どうもありがとう。

ピッカービューを閉じる

4

1 に答える 1

3

UIPickerView内部に挿入しないでくださいUIActionSheet。この方法では正しい動作が得られません。私の解決策:

  1. サブクラス化UIButton、メソッド実装canBecomeFirstResponder:、リターンYES
  2. inputViewサブクラス化されたUIButtonreturnにメソッドを実装しますUIPickerView
  3. 必要に応じて、サブクラス化されたボタンにメソッドを実装inputAccessoryViewして、ピッカーの上にツールバーを返します。
  4. 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
于 2012-08-05T10:07:56.037 に答える