Entering a Date in a UITextField using a UIDatePicker Part 2
Posted by admin in Data Entry Wednesday, 11 January 2012 06:42 15 Comments
This is the 2nd part of the series that covers changing the UITextField through its delegate to prevent the keyboard from displaying. In its place, a UIDatePickerView is displayed via a UIActionSheet to allow the user to scroll through the dates for data entry. The UIDatePickerView can easily be replaced by a generic UIPickerView or if used on an iPad a UIPopoverController to drop a list down (or up, etc.) from a UITextField.
@Nethfel I did it!
))))))
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if (birthTextField == textField) {
[self setBirth];
return NO;
} else
return YES;
}
by doing this i can show keyboard and datepicker differently. But i still have the sigbart erro due to the dismissDateSet but i did everything you did. what could have gone wrong?
@Nethfel I have also discovered my sigavrt error occurs when i click the set button and is cos of “unrecognized selector sent to instance”. do u have any idea how to solve it? I am currently very new to xcode so i hope you can help me out. Thank you!!
Hi can u show me an example? cos i am still stuck
Should i use the if else function in the textfieldshouldbeginediting?
@Nethfel thanks man i appreciate it.
@memo431 I’ll see what I can do – I haven’t had access to my laptop for a while now (which is what I use as a “teleprompter” once I’ve written a project for youtube) which is why I haven’t put up a new video, once I get it back I plan to start up again.
@WanJiinG You have to identify which text field you’re dealing with prior to popping up the date picker. In textFieldShouldBeginEditing: you need to test based upon the UITextField passed to it as a parameter whether you should pop the date picker or not. The easiest way is to tag the text fields and test the tag of the UITextField passed to see if it is the field that should pop the picker. In terms of the sigbart, you need to check the console to see what exactly is causing it.
Hi. How to make it selective for certain text field. cos i have 5 text fields and when i use ur method all my text fields become date picker and i keep getting a “sigabrt” error..
@nethfel can you do a tutorial on how to do it for a simple pickerview, when you click the texfield the picker shows and when you click away from it goes away.
@joeyfry Hmm, in all honesty, I couldn’t even comment without seeing the code as you’re attempting it. My only guess is that some object is being released that shouldn’t be after the set button is pressed OR you aren’t retaining (or maintaining a strong reference if using ARC) to the date that’s returned from the date picker. Those are my initial thoughts.
Nethfel, Thanks very much. The picker works flawlessly in it’s own view, but when I open it from within a view that’s in a UINavigationController that’s in a UITabbarController (HA), I can’t choose a date until I hit the SET button. and anything after that errors out. Do you know why this would be? I even tried adjusting the CGRectMake to take into account the top and bottom bars. Any help is appreciated! Thanks!
your the best
@pauldtyler Not a problem. You know, probably the easiest way to do the if statements would be to use tags for the text fields, then it’s a simple test of the tag of the text field with a specific integer and it would make it really easy to even use a switch statement as well unlike comparing objects which would require the use of multiple if statements
Complete system shut down seemed to do trick. Have your interface pulled in now to my app. Just need to work out if statements in textFieldShouldBeginEditing to selectively run on certain fields only. Thanks a lot for this example!
@pauldtyler Hi, this was done in XCode 4 What sort of problems did you have with your code exactly? Are you working with XCode 4.0.x (Snow Leopard), 4.1 (Lion) or 4.2 (beta)? There shouldn’t be a lot of changes unless you’re working in 4.2 with ARC turned on which will probably break most of the code (then again, one shouldn’t be using 4.2 for general work or project submission yet
)
Nice implementation. Was this in Xcode 3? Out of the gate, I had a string of problems just declaring the NSDate variable and the UIAction sheet in Xcode 4. Am I working with a corrupt file or is do I need to make a number of syntax changes to work in the later version?