Custom cell UITextField updated in all cells across all sections - ios

This is a dynamic prototype UITableView: In my cellForRowAtIndexPath:
I have dequequed my custom cell and this works just fine:
TimeSetViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:#"timeCell" forIndexPath:indexPath];
Next I set TimePicker to UITextField in timeCell and this works fine as well (i.e. the time picker displayed onclick of UITextField in timeCell) :
// Start Time
UIDatePicker *sTimePicker = [[UIDatePicker alloc]init];
sTimePicker.datePickerMode = UIDatePickerModeTime;
sTimePicker.backgroundColor = [UIColor whiteColor]
[cell.startTripTime setInputView:sTimePicker];
// End Time
UIDatePicker *eTimePicker = [[UIDatePicker alloc]init];
eTimePicker.datePickerMode = UIDatePickerModeTime;
eTimePicker.backgroundColor = [UIColor whiteColor];
[cell.endTripTime setInputView:eTimePicker];
Next I wanted to update the cell's UITextField through an event, in which I have understood from #Mani's post, I have set:
sTimePicker.tag = 1;
eTimePicker.tag = 2;
[sTimePicker addTarget:self action:#selector(updateTextField:)
forControlEvents:UIControlEventValueChanged];
[eTimePicker addTarget:self action:#selector(updateTextField:)
forControlEvents:UIControlEventValueChanged];
And in my custom method -(void)updateTextField:(UIDatePicker *)sender I have included this:
//Date Formatter
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setTimeStyle: NSDateFormatterShortStyle];
if(sender.tag == 1){
//Get Start Date
UIDatePicker *starttimepicker = (UIDatePicker*)sender.inputView;
thisStartTime = [starttimepicker date];
//Display Date
NSString *startTimeText = [dateFormat stringFromDate:thisStartTime];
sTimeText = [NSString stringWithFormat:#"%#",startTimeText];
}
if(sender.tag == 2){
//Get End Date
UIDatePicker *endtimepicker = (UIDatePicker*)sender.inputView;
thisEndTime = [endtimepicker date];
//Display Date
NSString *endTimeText = [dateFormat stringFromDate:thisEndTime];
eTimeText = [NSString stringWithFormat:#"%#",endTimeText];
}
Question Update
I managed to pass startTimeText and endTimeText to cell.startTripTime.text and cell.endTripTime.text through sTimeText and eTimeText.
And I used [_tableView reloadData]; in -(void)updateTextField:(UIDatePicker *)sender (after the two if blocks) and the data is displayed in my cell.startTripTimeand cell.endTripTime UITextField, but it appears the same in across of my sections, while I want it to change only the first section's time, how can I fix that?
//I couldn't include the image but the output of the UITableView looks like this:
_________________________
Section 1
_________________________
Start Time : 4:50 PM
_________________________
End Time: 5:50 PM
_________________________
Section 2
_________________________
Start Time : 4:50 PM
_________________________
End Time: 5:50 PM
_________________________
And the [_tableView reloadData]; seems to interfere and reloads the view while I haven't even finish picking the time that I wanted, how can I fix this too?

The action method should be sent to one of the picker, the startStripTime seems to be a UIView instance not a UIDatePicker instance.
It is he date picker that changes its value, not the view containing it.
You should change also the update method accordingly.

Related

Programmatically change title of UIButton not working in iOS Xcode-Beta 3

Hey I am working on a small app that changes the text of the UIButton based upon the current state of the date selector. It either will show "What day was it?" or "What day will it be?". However with xcode 6.3 beta, the UIButton SetTitle method does not seem to exist. Instead I try to assign a string to titleLabel.txt, but this does not change the button text.
Method is as follows:
(IBAction) changeButton:(id)sender {
NSDate *currentDate = [self.datePicker date];
NSDate *now = [NSDate date];
if(currentDate<now){
self.whatButton.titleLabel.text = #"What day was it?";
}
else {
self.whatButton.titleLabel.text = #"What day will it be?";
}
I have seen that the setTitle method would be a possible solution but discovered that the libraries may have been changed. Any suggestions would be greatly appreciated!
You should be setting your titles like this:
[self.whatButton setTitle:#"What day was it?" forState:UIControlStateNormal];
Use the following code to set the title of the button in different states as if for normal
[self.yourButton setTitle:#"Default Title" forState:UIControlStateNormal];
as if you have clicked your button for selected state it must be
[self.yourButton setTitle:#"Selected Title" forState:UIControlStateSelected];
as if you have clicked your button again for deselected state it must be
[self.yourButton setTitle:#"Selected Title" forState:UIControlStateDisable];
and many other states you can set different Text in every state of the button
Try changing the way you make the date comparison -
-(IBAction)changeButton:(id)sender {
NSDate *currentDate = [self.datePicker date];
NSDate *now = [NSDate date];
NSString *titleString = ([currentDate timeIntervalSinceReferenceDate] < [now timeIntervalSinceReferenceDate]) ? #"What day was it?" : #"What day will it be?";
[self.whatButton setTitle:titleString forState:UIControlStateNormal];
}
thank you for all your suggestions. The first suggestion was a little off base since it was not the sender's title that I wanted to change. The second suggestion is a good idea that I will use in the future. I ended up solving the riddle myself with this:
- (IBAction) changeButton:(id)sender {
NSDate *currentDate = [self.datePicker date];
NSDate *now = [NSDate date];
UIButton *tempBtn = (UIButton *)self.whatButton;
UIControlState state = tempBtn.state;
if([currentDate compare:now] == NSOrderedAscending){
[tempBtn setTitle:#"What day was it?" forState:state];
}
else {
[tempBtn setTitle:#"What day will it be?" forState:state];
}
}`

Custom UITableCell and UIDatePicker

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

UIDatePicker constraints not working in device

I am using UIDatePicker control in my project. Added the UIDatePicker in xib file, and programmatically setting the constraints such as minimumDate, maximumDate to the datePicker. This is working fine in simulator, but in device I am able to select any date in datePicker.
Sample code:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.datePicker setMinimumDate:[NSDate date]];
[self.datePicker setMaximumDate:[[NSDate date] dateByAddingTimeInterval:3*24*60*60]];
}
screenshot:
Note: I am working on xcode5 - DP6.
Thanks in advance.
Add an action for the UIControlEventValueChanged event on the UIDatePicker and implement checks to see if the selected date is greater than maximum date or less then the minimum date and set the date accordingly, like so:
- (IBAction)dateValueChanged:(id)sender
{
NSTimeInterval date = [self.datePicker.date timeIntervalSince1970];
NSTimeInterval maxmimumDate = [self.datePicker.maximumDate timeIntervalSince1970];
NSTimeInterval minimumDate = [self.datePicker.minimumDate timeIntervalSince1970];
if (date < minimumDate) {
self.datePicker.date = self.datePicker.minimumDate;
}
if (date > maxmimumDate) {
self.datePicker.date = self.datePicker.maximumDate;
}
// Use the selected date
NSLog(#"%#", self.datePicker.date);
}
This will cause the UIDatePicker to scroll to the max/min date if you select a date outside the range.

UIDatePicker Automation using KIF dateChangedAction not called

I'm automating date selection on UIDatePicker using KIF. I added the accessibility label and set the target for the picker if the date changes.
Ref: http://bit.ly/140ICwo
+(id) changeDate: (NSDate *) myDate
{
[s addStep:[KIFTestStep stepToEnterDate:myDate ToDatePickerWithAccessibilityLabel:#"datePicker"]];
[self wait:s timeInSeconds:3];
[s addStep:[KIFTestStep stepToTapViewWithAccessibilityLabel:#"Done" traits:UIAccessibilityTraitButton]];
return s;
}
- (void) ViewDidLoad
{
...
datePicker.maximumDate = lastAvailableDate;
datePicker.date = (dateValue ? dateValue : [NSDate date]);
[datePicker addTarget:self action:#selector(dateChangedAction:)
forControlEvents:UIControlEventValueChanged];
self.datePicker.accessibilityLabel = #"datePicker";
self.footerLabel.accessibilityLabel = #"datelabel";
}
- (IBAction)dateChangedAction:(id)sender
{
[dateValue release];
dateValue = [datePicker.date retain];
dateCell.detailTextLabel.text = [[[self class] sharedFormatter] stringFromDate:dateValue];
[self setDateTitleText:[[[self class] sharedFormatter] stringFromDate:dateValue]];
}
The Picker rotates and stops at the given date however the "dateChangedAction" function is not getting called, hence the label which displays the selected date is not getting updated.
If I run the app with out KIF everything works fine. Also I tried to manually select a date when running KIF to check it it updates the label but it seems like the UI gets frozen and I cannot click any UI controls.
Looks like the problem is related to this posting
http://bit.ly/10xtbqU
Any help is very much appreciated.
Thanks
I run into the same problem you're just missing
[picker sendActionsForControlEvents:UIControlEventValueChanged];
to trigger the dateChangedAction callback,
in other words try this:
+ (id)stepToEnterDate:(NSDate*)date ToDatePickerWithAccessibilityLabel:(NSString*)label
{
NSString *description=[NSString stringWithFormat:#"Enter date to Date picker with accessibility label '%#'",[date description]];
return [self stepWithDescription:description executionBlock:^(KIFTestStep *step, NSError **error)
{
UIAccessibilityElement *element = [[UIApplication sharedApplication] accessibilityElementWithLabel:label];
KIFTestCondition(element, error, #"View with label %# not found", label);
if(!element)
{
return KIFTestStepResultWait;
}
UIDatePicker *picker = (UIDatePicker*)[UIAccessibilityElement viewContainingAccessibilityElement:element];
KIFTestCondition([picker isKindOfClass:[UIDatePicker class]], error, #"Specified view is not a picker");
[picker setDate:date animated:YES];
// trigger the UIControlEventValueChanged in case of event listener
[picker sendActionsForControlEvents:UIControlEventValueChanged];
return KIFTestStepResultSuccess;
}];
}

iOS strange keyboard-hiding behavior when adding a subview

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;

Resources