gpt4 book ai didi

ios4 - 点击文本框时显示日期选择器

转载 作者:行者123 更新时间:2023-12-03 10:49:00 26 4
gpt4 key购买 nike

如何在点击文本框时显示日期时间选择器控件?

我有一个具有到达和离开文本字段的用户界面,当用户单击到达文本框时,它应该调出一个日期时间选择器控件而不是键盘,并且与离开文本框相同。

最佳答案

您可以使用 inputViewinputAccessoryView为此文本字段的属性。创建日期选择器并将其设置为两个文本字段的输入 View 。还要为 Done 创建另一个 View 按钮并将其作为附件 View 。您将需要该按钮来关闭输入 View 。
Done按钮必须连接到基本上执行此操作的功能 –

if ( [textField1 isFirstResponder] ) {
[textField1 resignFirstResponder];
} else if ( [textField2 isFirstResponder] ) {
[textField2 resignFirstResponder];
}

另一种选择是子类化 UITextField并覆盖 inputViewinputAccessoryView .这是当有大量它们时要走的路。

示例
@interface CustomKeyboardAppDelegate : NSObject <UIApplicationDelegate> {
...

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITextField *textField;
@property (nonatomic, retain) IBOutlet UIToolbar *accessoryView;
@property (nonatomic, retain) IBOutlet UIDatePicker *customInput;

- (IBAction)dateChanged:(id)sender;
- (IBAction)doneEditing:(id)sender;
@end

在 XIB 中,拉出一个 UIToolbar和一个 UIDatePicker但不要将其附加到 View 中。正确连接 socket 。 dateChanged:响应日期选择器和 doneEditing: 中的更改在 Done 时调用单击工具栏中的按钮。也连接它们。这些方法的实现如下所列。
@implementation CustomKeyboardAppDelegate

@synthesize window=_window;
@synthesize textField = _textField;
@synthesize accessoryView = _accessoryView;
@synthesize customInput = _customInput;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.textField.inputView = self.customInput;
self.textField.inputAccessoryView = self.accessoryView;
...
}

...

- (IBAction)dateChanged:(id)sender {
UIDatePicker *picker = (UIDatePicker *)sender;

self.textField.text = [NSString stringWithFormat:@"%@", picker.date];
}

- (IBAction)doneEditing:(id)sender {
[self.textField resignFirstResponder];
}
@end

由于更多的文本字段依赖于这个选择器,最后两种方法会变得臃肿。

关于ios4 - 点击文本框时显示日期选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6074683/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com