我已经尝试了所有的建议但是无法让UIDatePicker实际显示.我采取什么方法似乎并不重要.我尝试使用继承模型,我继承UITextBox控件并覆盖它的默认方法以显示UIDatePicker然后我确保在StoryBoard中我将我的UITextVIEw控件的类设置为该自定义类.我已尝试以编程方式生成UIDatePicker,并尝试将其拖到StoryBoard中的视图上.当我尝试编程方法时,不显示任何内容,当我尝试将其拖到StoryBoard上时,它总是会显示.当我将它的属性设置为“隐藏”时,它会隐藏但是当我尝试将代码添加到应该取消隐藏它的textBoxDIDBeginEditing方法时,我无法显示它.
我确保将UITextVIEw的inputVIEw属性设置为等于我的UIDatePicker控件.
什么都行不通!我不明白为什么Apple不只是将UIDatePicker作为UITextVIEw控件的Attributes Inspector下拉列表中的默认选项之一.这会让这更容易.
下面是我的实现类文件中的一些代码.
#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];}
这是我的头文件的样子……
#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)也不起作用.我完全按原样复制了here提供的代码.然后我将名为“DateFIEld”的自定义类添加到我的项目(标题和实现文件)中,然后在我的AddTaskVIEwController.h和AddTaskVIEwController.m文件中用DateFIEld替换dueDate的UITextBox声明,并确保StoryBoard引用通过在IDentity Inspector中为控件的类选择DateFIEld来更新.
无论我做什么,我都无法在点击UITextVIEw时显示UIDatePicker.当我设置断点时,它确实触及了dueDateDIDBeginEditing方法,因此我知道正在触发某些事情.问题是我不明白为什么没有使用UIDatePicker显示subVIEw.
任何帮助都会受到高度赞赏,因为这个看似简单的任务似乎需要花费很多时间.我可以做这种事情,闭着眼睛,双手绑在我的背后用Java和C#,但是当谈到Objective-C时,一切都是如此不必要的复杂.语法真的让我很恼火.
这么多方括号!!! Arghhhh!
解决方法 与显示键盘相比,为UITextFIEld的inputVIEw显示UIDatePicker相对较难,但与您遇到的麻烦相比相对容易.我注意到你的代码中遇到的第一个问题之一是,对于那些刚接触iOS / Mac开发的人来说,这是一个非常常见的问题,就是你试图设置一个尚不存在的对象的属性.放“self.dueDate.inputVIEw = datePicker;”在你的initWithNibname:bundle中:因为视图尚未加载而无法工作,dueDate是你视图的一部分.在objective-c中,使用alloc& amp;来实例化一个对象.在里面. Init是对任何对象的第一个实际方法调用.此时,在视图控制器的生命周期中,尚未创建视图.方法调用self.dueDate.inputVIEw = datePicker;属于vIEwDIDLoad方法.当它听起来像是被调用的时候会被调用,并且您的dueDate属性将在该点被正确加载.无需使用UITextFIEld的自定义子类来显示文本字段的日期选择视图.以下是自定义输入视图类的一个非常基本的示例:
ExampleBasicdateinputVIEw.h:
#import <UIKit/UIKit.h>@interface ExampleBasicdateinputVIEw : UIVIEw@property (strong,nonatomic) UIDatePicker *datePicker;@end
ExampleBasicdateinputVIEw.m:
#import "ExampleBasicdateinputVIEw.h"@implementation ExampleBasicdateinputVIEw{ UITextFIEld *_inputFIEld; // ivar to store the textfIEld currently being edited}@synthesize datePicker = _datePicker;// TARGET METHODS-(voID)pickerValueChanged:(UIDatePicker *)picker{ _inputFIEld.text = self.datePicker.date.description; // set text to date description}-(voID)vIEwDoubleTapped:(UITapGestureRecognizer *)tapGR{ [_inputFIEld resignFirstResponder]; // upon double-tap dismiss picker}-(voID)textFIEldBeganEditing:(NSNotification *)note{ _inputFIEld = note.object; // set ivar to current first responder}-(voID)textFIEldEndedEditing:(NSNotification *)note{ _inputFIEld = nil; // the first responder ended editing CRITICAL:avoIDs retain cycle}// INITI METHODS-(voID)initializationCodeMethod{ _datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,0)];// All pickers have preset height self.bounds = _datePicker.frame; // Make our vIEw same size as picker [self addSubvIEw:_datePicker]; [_datePicker addTarget:self action:@selector(pickerValueChanged:) forControlEvents:UIControlEventValueChanged]; // register to be notifIEd when the value changes // As an example we'll use a tap gesture recognizer to dismiss on a double-tap UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(vIEwDoubleTapped:)]; tapGR.numberOfTapsrequired = 2; // limit to double-taps [self addGestureRecognizer:tapGR]; NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; [center addobserver:self selector:@selector(textFIEldBeganEditing:) name:UITextFIEldTextDIDBeginEditingNotification object:nil]; // Ask to be informed when any textfIEld begins editing [center addobserver:self selector:@selector(textFIEldEndedEditing:) name:UITextFIEldTextDIDEndEditingNotification object:nil]; // Ask to be informed when any textfIEld ends editing}-(ID)init{ if ((self = [super init])){ [self initializationCodeMethod]; } return self;}-(ID)initWithFrame:(CGRect)frame{ if ((self = [super initWithFrame:frame])){ [self initializationCodeMethod]; } return self;}-(ID)initWithCoder:(NSCoder *)aDecoder{ if ((self = [super initWithCoder:aDecoder])){ [self initializationCodeMethod]; } return self;}-(voID)dealloc{ [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFIEldTextDIDBeginEditingNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFIEldTextDIDEndEditingNotification object:nil];}@end
然后在视图中加载你将使用这样的类:
ExampleBasicdateinputVIEw *dateEntryVIEw = [[ExampleBasicdateinputVIEw alloc] init];self.dueDate.inputVIEw = datePickerVIEw;
正如您在.h文件中看到的那样,我们已将日期选择器实例公开为属性,因此您可以设置样式.等等:
dateEntryVIEw.datePicker.datePickerMode = UIDatePickerModeDate;
显然这是一个非常基本的例子,但我认为它显示了可以做的事情.
总结以上是内存溢出为你收集整理的iphone – XCode:当用户点击UITextbox时显示UIDatePicker全部内容,希望文章能够帮你解决iphone – XCode:当用户点击UITextbox时显示UIDatePicker所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)