XCode:当用户点击UITextbox时显示UIDatePicker [英] XCode: Displaying a UIDatePicker when user clicks on UITextbox

查看:82
本文介绍了XCode:当用户点击UITextbox时显示UIDatePicker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经研究过这个话题,并且发现人们在很多网站上发布完全相同的问题,包括正确的这里在stackoverflow中。

I have already researched this topic to death and found people posting the exact same question on a number of websites including right here in stackoverflow.

我已经尝试了所有的建议但是无法让UIDatePicker实际显示。我采取什么方法似乎并不重要。我尝试使用继承模型,其中我继承UITextBox控件并覆盖它的默认方法以显示UIDatePicker然后我确保在StoryBoard中我将我的UITextView控件的类设置为该自定义类。我已尝试以编程方式生成UIDatePicker,并尝试将其拖到StoryBoard中的视图上。当我尝试编程方法时,没有显示任何内容,当我尝试将其拖到StoryBoard上时,它总是会显示。当我将它的属性设置为隐藏时,它会隐藏但是当我尝试将代码添加到应该取消隐藏它的 textboxDidBeginEditing 方法时,我无法显示它。
我已经确保将UITextView的inputView属性设置为等于我的UIDatePicker控件。

I have tried all of the suggestions but have been unable to get the UIDatePicker to actually display. It doesn't seem to matter what approach I take. I've tried using the inheritance model where I subclass the UITextBox control and override it's default methods in order to display the UIDatePicker and then I make sure that in StoryBoard I set the class of my UITextView control to be that custom class. I've tried programmatically generating the UIDatePicker and also tried dragging it onto the view in StoryBoard. When I try the programmatic approach nothing is displayed and when I try dragging it onto the StoryBoard it ALWAYS displays. When I set it's attribute to "hidden" it hides but then I can't get it to show even when I try to add code to the textboxDidBeginEditing method that should unhide it. I've made sure to set the UITextView's inputView property equal to my UIDatePicker control.

什么都行不通!我不明白为什么Apple不只是将UIDatePicker作为UITextView控件的Attributes Inspector下拉列表中的默认选项之一。这会让这更容易。

Nothing works! I don't understand why Apple didn't just make the UIDatePicker one of the default options in the drop down list in the Attributes Inspector for the UITextView control. That would have made this so much easier.

以下是我的实现类文件中的一些代码。

Below is some of the code from my implementation class file.

#import "AddTasksViewController.h"

@implementation AddTasksViewController

@synthesize dueDate;
@synthesize description;
@synthesize shortTitle;
@synthesize datePicker;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.dueDate.inputView = datePicker;

    }
    return self;
}

- (IBAction)dueDateDidBeginEditing:(UITextView *)textField 
{  
    [textField resignFirstResponder];
    [datePicker setFrame:CGRectMake(0,200,320,120)];
    [datePicker addTarget:self action:@selector(done) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:datePicker];
}

这是我的头文件的样子......

Here is what my header file looks like...

#import <UIKit/UIKit.h>

@interface AddTasksViewController : UIViewController

@property (nonatomic, copy) IBOutlet UITextView      *dueDate;
@property (nonatomic, copy) IBOutlet UITextView      *description;
@property (nonatomic, copy) IBOutlet UITextView      *shortTitle;
@property (nonatomic, retain) IBOutlet UIDatePicker    *datePicker;

- (IBAction)doneEditing:(id)sender;
- (IBAction)dateChanged:(id)sender;
- (IBAction)dueDateDidBeginEditing:(UITextView *)textField;

@end

正如我已经提到的,另一种方法是我接受(即子类化UITextView)也不起作用。我完全复制了此处提供的代码。然后我将名为DateField的自定义类添加到我的项目(标题和实现文件)中,然后在我的AddTaskViewController.h和AddTaskViewController.m文件中用DateField替换dueDate的UITextBox声明,并确保StoryBoard引用通过在Identity Inspector中为控件的类选择DateField来更新。

As I've already mentioned, the other approach that I took (i.e. subclassing UITextView) didn't work either. I copied the code provided here exactly as it was show. I then added the custom classes called "DateField" to my project (both the header and implementation files) and then replaced the UITextBox declaration for the dueDate with DateField in both my AddTaskViewController.h and AddTaskViewController.m files and made sure that the StoryBoard references were updated to by selecting DateField for the control's class in the Identity Inspector.

无论我做什么,我都无法在单击UITextView时显示UIDatePicker。当我设置一个断点时,它会点击 dueDateDidBeginEditing 方法,所以我知道正在触发某些事情。问题是我不明白为什么没有使用UIDatePicker显示subView。

No matter what I do I cannot get the UIDatePicker to display upon clicking on the UITextView. When I set a breakpoint it does hit the dueDateDidBeginEditing method so I know that something is being triggered. The problem is that I don't understand why a subView is not being showing with the UIDatePicker.

任何帮助都会非常感激,因为这个看似简单的任务似乎正在采取比它应该多得多。我可以做这种事情,闭着眼睛,双手绑在我的背后用Java和C#,但就Objective-C而言,一切都是如此不必要的复杂。语法真的让我很恼火。

Any help would be greatly appreciated as this seemingly straightforward task seems to be taking much, much longer than it should. I could do this sort of stuff with my eyes closed and hands tied behind my back in Java and C# yet everything is so unnecessarily complicated when it comes to Objective-C. The syntax really irritates me.

这么多方括号! Arghhhh !!!

So many square brackets!!! Arghhhh!!!

推荐答案

我在使用Storyboard时遇到了同样的问题。以下是我如何解决它:

I had the same issue using Storyboard. Here is how I resolved it:

概述:
我在viewDidLoad中分配并初始化我的UIDatePicker属性:然后我将textfield的inputView属性设置为我的datepicker属性。

Overview: I alloc and initialize my UIDatePicker property in viewDidLoad:. I then set the inputView property of my textfield to my datepicker property.

这是我的代码:
在TripViewController.h中:

Here is my code: In TripViewController.h:

@property (nonatomic,strong) IBOutlet UIDatePicker *datePicker;

在TripViewController.m中:

In TripViewController.m:

@synthesize datePicker;
...
-(void)viewDidLoad {
...
self.datePicker = [[UIDatePicker alloc]init];
[self.datePicker addTarget:self action:@selector(dateChanged) forControlEvents:UIControlEventValueChanged];
self.dateField.inputView = self.datePicker;
}

然后我实现我的方法dateChanged,以便每当日期选择器轮停止移动时,我的文本字段已更新。

I then implement my method, dateChanged so that whenever the date picker wheel stops moving, my textfield is updated.

- (void)dateChanged
{
NSDate *date = self.datePicker.date;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateStyle:NSDateFormatterMediumStyle];
self.dateField.text = [dateFormat stringFromDate:date];
}

这篇关于XCode:当用户点击UITextbox时显示UIDatePicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆