I'm very new to iOS so obviously I'm missing a few concepts when trying to follow this solution Display datepicker on tapping on textfield to my problem.
I have the interface and implementation in a class called DatepickerAppDelegate, but I'm lost here:
"In the XIB, Pull out a UIToolbar and a UIDatePicker but don't attach it to the view. Connect the outlets appropriately. dateChanged: responds to changes in the date picker and doneEditing: is called when the Done button in the tool bar is clicked. Connect them too. The methods are implemented as listed below."
I read something about XIB's but I'm using a storyboard, so I think I don't have then. Also, I'm not sure how I'm supposed to pull out elements without attaching it to my view controllers (I'm trying, but when I release the mouse button, the tool flies back to the toolbar), and I don't even understand why I need a UIToolbar.
And finally, how do I connect my textfield to the datepicker delegate?
Can someone help me?
UPDATE:
In fact it is very simple, I just need to:
datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:#selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
self.birthday.inputView = datePicker;
UIResponder, from which UITextField inherits, defines a property called inputView. This property (a UIView *) can define a view that will be displayed instead of the keyboard when that UIResponder becomes the first responder.
Thus, if you want a UIDatePicker to appear when you tap in a textField (ie, you make that textField the first responder), then you can say, naïvely:
UIDatePicker *datePicker = [[UIDatePicker alloc] init...];
... configure datePicker ...
[myTextField setInputView:datePicker];
Now, when your UITextField is tapped, a UIDatePicker will be brought up instead.
HOWEVER
You'll want to do more than this, because you'll need to handle different orientations and different idioms.
But that's the basic idea.
(And if you want a toolbar above the UIDatePicker, that's what the inputAccessoryView property is for...)
In viewDidLoad of ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
UIDatePicker *datePicker = [[UIDatePicker alloc]init];
[datePicker setDate:[NSDate date]];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:#selector(dateTextField:) forControlEvents:UIControlEventValueChanged];
[txtFieldBranchYear setInputView:datePicker];
}
Add one method in your ViewController
-(void) dateTextField:(id)sender
{
UIDatePicker *picker = (UIDatePicker*)txtFieldBranchYear.inputView;
[picker setMaximumDate:[NSDate date]];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSDate *eventDate = picker.date;
[dateFormat setDateFormat:#"dd/MM/yyyy"];
NSString *dateString = [dateFormat stringFromDate:eventDate];
txtFieldBranchYear.text = [NSString stringWithFormat:#"%#",dateString];
}
txtFieldBranchYear is an outlet of UITextField
Add a datepicker view and uitoolbarview to your view.
For example
CGFloat toolbarHeight = 44.f;
CGFloat datePickerHeight = 140.f;
CGFloat containerViewHeight = toolbarHeight+datePickerHeight;
//create container view for datepicker and toolbar
UIView *containerView = [[UIVIew alloc] initWithFrame:CGRectMake(0.f, [self.view bounds].size.height+containerViewHeight, [self.view bounds].size.width, containerViewHeight)];
//after init toolbar set the frame
toolBar.frame = CGRectMake(0.f,0.f,containerView.frame.size.width, toolbarHeight];
//then add on toolbar button save and release it after add
//after init datePicker set the frame
datePicker.frame = CGRectMake(0.f, toolbarHeight, containerView.frame.size.width, datePickerHeight);
//add datePicker and toolBar to containerView
[containerView addSubview: toolBar];
[containerView addSubview: datePicker];
[toolBar release];
[datePicker release];
//add containerView to main view
[self.view addSubview: containerView];
[containerView release];
So when view did load they are will be hidden Y of contener view is outside of the your view frame.
Now you should assign property delegate of UITextFieldDelegate
textfield.delegate = self;
In the method of delegate textFieldDidBeginEditing see UITextFieldDelegate you should fire method that will show your datePicker and toolbar.
If you have many textFields to define which is was selected use tag property, which is integer and you can use switch instead of many if conditions.
To show your datePicker and toolbar you should change the Y of containerView frame.
Here is how to do that
CGRect containerViewFrame = containerView.frame;
containerViewFrame.y -=containerViewHeight;
containerView.frame = containerViewFrame;
To hide just plus the containerViewHeight variable.
You can use UIViewAnimations to show it with some animation. For example UIViewAnimationCurveEaseInOut
Related
If VIN is an text field this code works for me to Hiding keyboard without breaking UITextField functionality..
UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
VIN.inputView = dummyView;
But in another case i need to do same thing for UIVIEW.. it is possible to do for UIView ?
UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
VIN.inputView = dummyView;
In this case VIN Is an UIView.. i am getting error like "Assignment to read only property".
UITextfield has inputview. Default inputview is keyboard. uiview has not input view that you can set so you can't assign input view to UIView.
And for hiding keyboard you should call resignFirstresponder like,
[myTextField resignFirstResponder];
This will hide keyboard and when you click textfield it will show keyboard again so you should do like this.
Hope this will help :)
If you want to hide or disable keyboard for particular textfield , you can use following textfield delegate
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
// Here You can do additional code or task instead of writing with keyboard
if (textField == YourTextField)
{
return NO;//Keyboar Wont appear for textfield
}
return YES;
}
I launch a keyboard-like interface for UIDatePicker using this code:
_meetingtimeTF.delegate = self;
datePicker = [[UIDatePicker alloc]init];
[datePicker setDatePickerMode:UIDatePickerModeDateAndTime];
[datePicker addTarget:self action:#selector(onDatePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
_meetingtimeTF.inputView = datePicker;
Is there a way to customize the size? Currently the white panel take up the whole horizontal screen even though the real interface is 1/3 of the screen.
How to dismiss the interface after I am done with date picking and click a button? I tried [datepicker setHidden:YES] but but sometime it just hide the datePicker and the white panel remain there.
I have an UITextView and one of my methods requires to change the UITextView inputView to an UIDatePicker, so the user can select a date for a reminder.
Here is the code I used
self.datePicker = [[UIDatePicker alloc] init];
self.noteTextView.inputView = self.datePicker;
[self.noteTextView becomeFirstResponder];
everything works perfectly, but I can't manage to switch back from datePicker to the default keyboard. What I have tried is the following code:
self.noteTextView.inputView = UIKeyboardTypeDefault;
Set the input view to nil
self.noteVite.inputView = nil;
I'm using storyboard and i want to add a popup to my app.
My popup will contain a slider (which i need to get his value), and also cancel option ('x' at top of the popup).
What is the best way to do that?
ViewController?
Xib file?
View?
Thanks in advance!
I think it's better to create a popup, button & slider in your code. Than you have more control over it. It's easier to access your slider & to dismiss the popover in the same code.
Put this code in the IBAction function when a button is pressed from where you want the popover:
//Create a popovercontroller
UIPopovercontroller *popOverController;
//Create an instance of the class you working in, your viewcontroller
YourViewController *popOverContent = [[YourViewController alloc] init];
//Create the popover view, you can change the size of it
UIView *popOverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
UISLider *slider = [[UISlider alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
UIButton *close = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 20)]
//Add slider & button to view
[popOverView addSubview:slider];
[popOverView addSubview:button];
//Add view to viewcontroller
[popOverContent addSubview:popOverView];
//Alloc & init the popovercontroller
popOverController = [[UIPopoverController alloc] initWithContentViewController:popOverContent];
//Set content size of popover
popOverController.popoverContentSize = CGSizeMake(200, 200);
//present popup from a button or frame
[popOverController presentPopoverFromRect:self.yourButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
Than create an IBAction for the close button and use this line of code to dismiss the popover:
[popOverController dismissPopoverAnimated:YES];
You can find more information about popOverControllers here
Depending on what you want to do with your popup and how do you want to present it you can choose:
ViewController from storyboard if you want to present a popup in modal style and from that popup you want to show other view controllers.
Xib file for a custom UIView, if you are using the popup to display some information and get the user input after that you will dismiss the popup and the popup will be used in several places (different view controllers)
Custom UIView in one storyboard view controller, if you want to display some information and get the user input and the popup will be use only on one place (one ViewController)
I need to know if a date picker can be presented like an alert view upon the click of a button. So far, I have hidden the date picker in viewdidload method and set hidden=no in the method for the button.
I intend to store the date within Core Data.
Create the pickerView, place it outside of your view bounds, add it as a subview and use animation to slide it up. Something like the below should work...
CGRect frame = CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, 215);
UIPickerView *typePicker = [[UIPickerView alloc] initWithFrame:frame];
[self.view addSubview:typePicker];
[UIView beginAnimations:#"slideIn" context:nil];
[typePicker setCenter:CGPointMake(typePicker.center.x, self.view.frame.size.height - typePicker.frame.size.height/2)];
[UIView commitAnimations];
You put the date picker in a subview and then present the subview at the click of a button. You probably want the subview to slide in from the side or bottom.