I created an IBOutlet for a UIPicker and named it 'RequestedDate.' I then went on the type
_RequestedDate.delegate = self;
but I keep getting the error:
property 'delegate' not found on object type 'UIDatePicker'
I am relatively new with Xcode and am just starting to work with code. Is there something I am doing wrong or cannot I not have a delegate for a UIPicker?
Thanks~
UIDatePicker uses the target-action pattern instead of the delegate pattern.
You either add a IBAction for the 'value changed' event in Interface Builder or you do it in code like this:
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
[datePicker addTarget:self action:#selector(datePickerDidChange:) forControlEvents:UIControlEventValueChanged];
// ...
Then you implement the action method, and can retrieve the new date.
- (void)datePickerDidChange:(UIDatePicker *)picker {
NSDate *newDate = picker.date;
NSLog(#"Picker changed date to %#", newDate);
}
UIPickerView and UIDatePicker only look similar, they are completely separate classes. UIPickerView is a subclass of UIView and UIDatePicker is a subclass of UIControl, they are not related.
I don't think UIDatePicker has a delegate property. https://developer.apple.com/library/ios/documentation/uikit/reference/UIDatePicker_Class/Reference/UIDatePicker.html
What are you trying to do exactly?
Related
I am using UISearchController programmatically, While tapping on Searchbar textField I want to be set Date picker as inputView
#property (strong, nonatomic) UISearchController *searchController;
References for text field : iPhone display date picker on UITextField touch
But unable to able to access Search bar Textfield. Please help.
You can get search textField using following code. Write it in viewDidLoad
UITextField *searchBarTextField = [self.searchController.searchBar valueForKey:#"_searchField"];
[searchBarTextField setInputView:datePicker];
Hope this will help you
First set delegate of UISearchBar then you can use following delegate method :
- (BOOL)searchBarShouldBeginEditing:(UISearchBar*)searchBar {
[self showDatePicker];
return NO;
}
-(void) showDatePicker {
UIDatePicker *datePicker = [[UIDatePicker alloc]init];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker setDate:[NSDate date]];
[datePicker addTarget:self action:#selector(showSelectedDate:) forControlEvents:UIControlEventValueChanged];
}
- (void)showSelectedDate:(id)sender {
UIDatePicker *picker = (UIDatePicker*)self.date.inputView;
searchBar.text = [self formatDate:picker.date];
}
You can use any date formatter to show date in desired format.
I have created a custom UITableViewCell and assigned it to a custom class.
The custom cell has two labels and one uitextfield in it. Everything works great, but I need to be able to edit the text field with a date picker. I can't seem to figure out how to make the textfield's inputview the date picker? I can do it fine in a regular cell, but can't find a way to access it in my custom cell?
NSDate *eventDt= [gregorian dateByAddingComponents:offsetComponents toDate:[self trialDate] options:0];
// NSLog(#"EVENT DATE: %#", eventDt);
NSDateFormatter* dateF = [[NSDateFormatter alloc] init];
[dateF setDateFormat:#"MMMM dd, yyyy"];
[dateF setTimeZone:[NSTimeZone localTimeZone]];
NSString *eventDtforDisplay = [dateF stringFromDate:eventDt];
resultscell.labelEventDesc.numberOfLines=0;
[resultscell.labelEventDesc sizeToFit];
[resultscell.labelEventDate setText:[NSString stringWithFormat:#"%#",eventDtforDisplay]];
this is my problem----> [resultscell.textEventDate.inputView = [self datePicker];
[resultscell.textEventDate setText:[NSString stringWithFormat:#"%#",eventDtforDisplay]];
return resultscell;
}
In custom cell you can do this very well in the constructor (init method) of the cell as parameter, or create a setter method. so you don't have to touch the UITextField in the parent class, but only handle the inputView in the cells setter method.
Seems also like handling with delegates and protocols could be usefull here. If you don't know how, here a small example: Creating uitableview forms with uitextfields
I just put UITextField in NavigationBar using xCode. But when I try to create IBOutlet binding my programm receive -[UITextField isEqualToString:]: unrecognized selector sent to instance.
Any suggestions?
Thnx.
You have to call it on the UITextField's text property:
UITextField *myTextField = [[UITextField alloc] initWithFrame:...];
myTextField.text = #"someString";
...
[myTextField.text isEqualToString:#"someString"]
Try this
-(void)viewDidAppear:(BOOL)animated{
UITextField *txtField=[[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
[txtField setBorderStyle:UITextBorderStyleRoundedRect];
txtField.text=#"Hello";
[self.navigationController.navigationBar addSubview:txtField];
}
Will look like..
UITextField *textField = [[UITextField alloc] init];
// configure text field
self.navigationItem.titleView = textField
Of course you could also use a property in your view controller instead of a local variable.
isEqualToString is method of string class not UITextField. Thatswhy you get this error. You are passing method to wrong object. Use textField.text instead of textField alone
Please provide some sample code or a screenshot if you want a good answer. May I suggest you read this document for future questions.
This is clearly a case of you calling isEqualToString on a UITextField when this is a method that must be called on a NSString.
I have a view with an UITextField and need to show an UIDatePicker (it's hidden below the keyboard) when a button is pressed.
I'm lazily instantiating the date picker because this allows me to show the view way more fast.
This is the code associated with the tap on the button:
- (IBAction)hideKeyboard {
if (!self.datePicker) {
// UIDatePicker allocation and initialization
self.datePicker = ...
...
[self.view addSubview:self.datePicker];
}
[self.textField resignFirstResponder];
}
What happens is that this interfere with the keyboard hiding animation. Actually there is no animation at all.
Another clue I have is that this doesn't happen on the simulator, but only on the actual device (an iPhone 4S).
How can I solve this?
EDIT:
Setting the datePicker as the inputView of the textField doesn't solve my problem.
I can recommend you another solution then hiding keyboard and using button for showing the date picker.
If I understood you correctly you want to show a date picker when user taps on the textfield and then maybe you set the date in textfield.
UITextField has a property
#property (readwrite, retain) UIView *inputView
If you set the inputView property to date picker then the textfield will show date picker instead a keyboard (and even animated like keyboard).
Of course the values need to get your own and set to textfield.
Edit reason: Question edited
I do not know why setting the datepicker as the input view did not help. Anyway if you do not want the keyboard shown and show the datepicker by adding a subview you can use the following delegate method of UITextField
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
implement the delegate method. In the method just add pickerview as subview to your view and return NO for not to show the keyboard.
Edit reason: Commend
Is there any reason why you do not use textfield? If not I would use it, because it makes everything easier. If you want to use for some reason the UILabel then you can watch the keyboard notification. When did the keyboard disapeared and then show the datepicker.
For keyboard notifications please refer here, good explained with sample codes, UIKeyboardDidHideNotification should help you
http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html
Quite simple :
Just add a date picker object as the input value of your textfield. No need to show/hide the keyboard.
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar] ;
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[NSDateComponents alloc] init] ;
[comps setYear:anyInteger];
NSDate *maximumDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[comps setYear:anyInteger;
NSDate *minimumDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[datePicker setMaximumDate:maximumDate];
[datePicker setMinimumDate:minimumDate];
datePicker.datePickerMode = UIDatePickerModeDate;
textField.inputView = datePicker;
I have a table view controller with (among others) a cell that is to represent a date. I followed this post "How do I make a modal date picker that only covers half the screen?" and I have it working with one big exception - I can't get the picker to disappear!
I tried registering for the event UIControlEventTouchUpOutside, but it seems that this is not generated by the picker (or at least in the mode that I am using it).
How can I recognize that the user has finished selecting a date?
Also, is there a way to disable the user from inputting directly into the UITextField? I want to force them to use the picker. I saw this post "Disable blinking cursor in UITextField?", but is there another way?
Reagards,
--John
Try this code.. here I am putting an datepicker to a uitextfield.. it will have a done button at the top right navigation bar.. so by clicking done I will user can dismiss the datepicker.. the another best method is by putting a toolbar above the datepicker having the done button.. Try this it will work.. when changing the datepicker you can populate the text field.. Hope this helps..
see this stackoverflow link https://stackoverflow.com/a/4824319/763747 this will have the datepicker with done button as toolbar above the keybord..
#pragma mark -
#pragma mark - TextField Delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return TRUE;
}
- (void) textFieldDidBeginEditing:(UITextField *)textField {
itsRightBarButton.title = #"Done";
itsRightBarButton.style = UIBarButtonItemStyleDone;
itsRightBarButton.target = self;
itsRightBarButton.action = #selector(doneAction:);
if ([textField isEqual:itsIncidentDateTextField])
{
itsDatePicker = [[[UIDatePicker alloc] init] autorelease];
itsDatePicker.datePickerMode = UIDatePickerModeDate;
[itsDatePicker addTarget:self action:#selector(incidentDateValueChanged:) forControlEvents:UIControlEventValueChanged];
//datePicker.tag = indexPath.row;
textField.inputView = itsDatePicker;
}
}
- (IBAction) incidentDateValueChanged:(id)sender{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM d, yyyy"];
itsIncidentDateTextField.text = [dateFormatter stringFromDate:[itsDatePicker date]];
[dateFormatter release];
}
I used a different method for disappearing the UIDatePicker.
create a XIB (nib) file and add to it a UIDatePicker then resize the view so it fits only the UIDatePicker, create a property (make it strong and nonatomic) in your ViewController (or whatever class your using, and synthesize of course).
#property (nonatomic, strong) UIView *myDatePickerView;
#synthesize myDatePickerView;
then create a loadDatePickerView method
- (void) loadDatePickerView
{
UINib *nib = [UINib nibWithNibName:kNIBname bundle:[NSBundle mainBundle]];
NSArray *views = [nib instantiateWithOwner:self options:nil];
self.myDatePickerView = [views firstObject];
[myDatePickerView setFrame:CGRectMake(0, 318, 320, 162)];
[self.view addSubview:myDatePickerView];
}
implement the UITextFieldDelegate, and in the textFieldDidBeginEditing method call the loadDatePickerView method,
[self loadDatePickerView];
to make function create a property which is a UIDatePicker instance
#property (nonatomic,strong)
UIDatePicker *myDatePicker;
(synthesize of course)
now create an IBAction like so:
-(IBAction)datePickerValueChanged:(UIDatePicker *)sender
{
myDatePicker = [[myDatePickerView subviews] lastObject];
//now you can do whatever you want with the DatePicker
}
now connect the IBAction to the picker in the XIB file, that way the XIB is now the UIDatePicker instance you created in the VC, if you want it to disappear you can add a UITapGestureRecognizer (in the ViewDidLoad) and the selector will be another IBAction which removes myDatePickerView from its' superView like this:
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dropPicker:)];
[self.view addGestureRecognizer:tap];
[self datePickerValueChanged:myDatePicker];
}
-(IBAction)dropPicker:(id)sender
{
[myDatePickerView removeFromSuperview];
}