从 InputAccessoryView 上的按钮更改 uidatepicker 的值
Change value of a uidatepicker from a button on the InputAccessoryView
我有一个 UIDatePicker
,它是在用户点击我视图中的文本字段时以编程方式创建的(作为第一响应者)。这发生在 for 循环内,所以我的代码中没有对这个选择器的引用。我在选择器顶部还有一个工具栏,它作为 inputAccessoryView
添加到选择器。我有一个 Done 按钮,可以让第一响应者辞职。到目前为止一切顺利。
除了 Done 按钮之外,我想在工具栏上添加另一个按钮,该按钮将更改选择器的值。没有 UIDatePickerDelegate
可以让我跟踪活动的选择器。
我总是可以为我的每个选择器定义 class 变量,从而手动保存引用。但是有没有一种简单的方法可以让我直接从 inputAccessoryView
访问 inputView
?
这个在 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
}
我有一个 UIDatePicker
,它是在用户点击我视图中的文本字段时以编程方式创建的(作为第一响应者)。这发生在 for 循环内,所以我的代码中没有对这个选择器的引用。我在选择器顶部还有一个工具栏,它作为 inputAccessoryView
添加到选择器。我有一个 Done 按钮,可以让第一响应者辞职。到目前为止一切顺利。
除了 Done 按钮之外,我想在工具栏上添加另一个按钮,该按钮将更改选择器的值。没有 UIDatePickerDelegate
可以让我跟踪活动的选择器。
我总是可以为我的每个选择器定义 class 变量,从而手动保存引用。但是有没有一种简单的方法可以让我直接从 inputAccessoryView
访问 inputView
?
这个在 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
}