0

UIDatePickerユーザーがビューのテキストフィールドをタップすると、プログラムで(最初のレスポンダーとして)作成される があります。これは for ループ内で発生するため、コード内にこのピッカーへの参照がありません。また、ピッカーに追加されたピッカーの上にツールバーがありinputAccessoryViewます。最初の応答者を辞任する [完了] ボタンがありますここまでは順調ですね。

ピッカーの値を変更する [完了] ボタンのほかに、ツールバーに別のボタンを追加したいと考えています。UIDatePickerDelegateアクティブなピッカーを追跡できるものはありません。

各ピッカーのクラス変数をいつでも定義できるため、参照を手動で保存できます。inputViewしかし、inputAccessoryView直接アクセスできる場所でこれを行う簡単な方法はありますか?

4

1 に答える 1

0

これはsome.hファイルにあります

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

{
     IBOutlet UIDatePicker *picker1;
     IBOutlet UITextField *txtFld;

}
@property (nonatomic, retain) UIToolbar *keyboardToolbar;

@end

some.m ファイル内

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
 picker1=[[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];//frames are just for demo
 [txtFld setInputView:picker1];
}

- (void)keyboardWillShow:(NSNotification *)notification
{
    if(keyboardToolbar == nil) {
        keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 410, 320, 44)] ;
        [keyboardToolbar setBarStyle:UIBarStyleBlackTranslucent];
        [keyboardToolbar sizeToFit];

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.4];

        UIBarButtonItem *flexButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];

        UIBarButtonItem *doneButton1 =[[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(resignKeyboard)];
        NSArray *itemsArray = [NSArray arrayWithObjects:flexButton,doneButton1, nil];

        [keyboardToolbar setItems:itemsArray];


        [txtFld setInputAccessoryView:keyboardToolbar];
        [self.view addSubview:keyboardToolbar];
        [UIView commitAnimations];
    }
}



-(void)resignKeyboard {

    [keyboardToolbar removeFromSuperview];
    [txtFld resignFirstResponder];
///do nescessary date calculation here

    }
于 2016-05-05T05:45:11.040 に答える