Two UIButtons calling the same method have different results - iphone

I have a UIButton that displays a time, and this time is displayed in the titleLabel property of the UIButton. To change this time, you can click on the button, which calls this method that brings in a date picker.
- (IBAction)slidePickerIn:(id)sender {
// Ok button to dismiss picker
[self.view bringSubviewToFront:self.datePickerOK];
// Bring picker to front
[self.view bringSubviewToFront:self.datePicker];
// animate entrance of picker and fade in of dismiss button
[UIView animateWithDuration:.5 animations:^{
[self.datePicker setFrame:pickerIn];
self.datePickerOK.alpha = 1;
}];
}
When you click on the dismiss button, this method gets called. It gets rid of the date picker and the dismiss button and it updates the title of the button displaying the time.
- (IBAction)slideDatePickerOut {
[UIView animateWithDuration:.5 animations:^{
self.datePickerOK.alpha = 0;
[self.datePicker setFrame:pickerOut];
}];
// Set time correctly
NSDate *wake = self.datePicker.date;
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"hh:mm a"];
self.wakeUpTime.titleLabel.text = [NSString stringWithFormat:#"%#", [df stringFromDate:wake]];
self.wakeUpTime.date = wake;
}
There is another button on the screen that also calls slidePickerIn and allows you to change the time.
THE PROBLEM: When I click on this other button to change the date displayed, everything works fine. However, when I click on the button itself, that contains the title showing the time, the date goes to 00:00 AM (the default value) while the date picker is displayed. When the date picker is dismissed, the time updates correctly. But as I said, using the other button doesn't change the value of the time while the picker is displayed. I am really unsure of why this could be. Any help would be much appreciated.

In your slidePickerIn add:
if(self.datePicker.alpha == 0)
{
//Add all the methods
}
In your slidePickerOut add:
if(self.datePicker.alpha == 1)
{
//Add all the methods
}
Now the UIButtons will be called in the correct time.

Make sure you have not assigned both methods to button

Related

Can't Select today's date on uidatepicker ( already pre-selected)

I added a UIDatePicker in my IOS app which get loaded by default with today's date selected.
I'm listening to the selected event to update my field but as today's is already selected I never get it today is the date I want to selec( because it is already selected). I need to change at least one of the column to soemthing different , and in a second step select back today's date.
Anyway to display the datepicker without a selected day or somehow listeng to the tab on the picker instead of the selection ?
Wouldn't like to add a Todays button in the toolbar on adding any external control to solve this, or start with a different day pre-selected as it iwll be the same issue with the pre-selected date.
Thanks
I am not 100% sure whether you are looking for this or not. But this works fine for me.
in the viewcontroller.m
- (void)viewDidLoad
{
[super viewDidLoad];
dp.date = [NSDate date]; //dp is datepicker object
NSDateFormatter *formDay = [[NSDateFormatter alloc] init];
[formDay setDateFormat:#"dd/MM/yyy HH:mm"];
NSString *day = [formDay stringFromDate:[dp date]];
txt.text = day;
}
-(IBAction)datepick:(id)sender
{
NSDateFormatter *formDay = [[NSDateFormatter alloc] init];
[formDay setDateFormat:#"dd/MM/yyy HH:mm"];
NSString *day = [formDay stringFromDate:[dp date]];
txt.text = day;
}
connect the method to value changed event of date picker and include UITextFieldDelegate
when the view is loaded,
when the date picker is rolled
Apple did not implement any notifications for this. The reason being is that the user will tap, spin, and change the date picker until they find the date they want. Then they would hit some sort of done or next button. You would not want the user to be trying to select a date and the picker keep dismissing. If you really want to implement this then you can subclass UIDatePicker or and capture touches on the section you want.
Try subclassing UIDatePicker, & overriding hitTest:withEvent:
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
// intercept the user touching the initially presented date on a datePicker,
// as this will normally not send an event.
// here we check if the user has touched the middle section of the picker,
// then send the UIControlEventValueChanged action
CGFloat midY = CGRectGetMidY(self.bounds);
CGFloat dY = fabs(midY - point.y);
if (dY < 17.0) // the active section is around 36px high
{
NSSet *targets = self.allTargets;
for (id target in targets)
{
NSArray *actions = [self actionsForTarget:target forControlEvent:UIControlEventValueChanged];
for (NSString *action in actions)
{
// suppress the leak warning here as we're not returning anything anyway
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[target performSelector:NSSelectorFromString(action) withObject:self];
}
}
}
return [super hitTest:point withEvent:event];
}
I'm sure it could be improved, but as a starting point it might help you.

UIDatePickerView as FirstResponder for UITextField is missing Month Component

I'm implementing a table view where for one particular table view cell, there is a textfield whose first responder needs to be a UIDatePicker. I've been able to implement most of it, but for some reason the view is not appearing completely correct. I've been stuck on this problem for a while and I've been looking at this answer for help.
UIDatepicker in UItextfield in UItableView
As of now, I have the data picker appearing and disappearing properly, the delegate for the text field is working properly, and the textfield can be updated. My problem is that for some reason the Month component of the date picker is missing (I have a picture, but since I have less than 10 rep, I can't post it). What it looks like is a typically UIDatePicker but the first component that has the months, is completely empty and can't scroll. The day and year components operate normally.
This is how I create the picker view
cell = [tableView dequeueReusableCellWithIdentifier:InfoIdentifier];
UILabel *textLabel = (UILabel *)[cell viewWithTag:kInfoLabelTag];
UITextFieldWithIndex *cellText = (UITextFieldWithIndex *)[cell viewWithTag:kInfoTextboxTag];
cellText.indexPath = indexPath;
textLabel.text = #"Birthday";
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:#selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
datePicker.tag = indexPath.row;
cellText.inputView = datePicker;
And here is the callback method
-(void)datePickerValueChanged:(id)sender {
NSLog(#"Date Changing");
currentTextField.text = #"new date";
}
Resigning the first responder works, and if I print the date, the month is present, so the only problem is that the month labels are missing.
I created this project using Storyboards, and I am also using core date in this project. However, the core data part of this view controller hasn't been implemented yet.
Any ideas?
So after looking through my code more I figured out that the problem was a category I created for a UIPickerView.
In another file, I created a custom picker view by using a category to override the drawrect: method of UIPickerView. In the overrided method, I actually set some of the views as hidden for a UIPickerView. Although the category was only imported in the other file, the category overrided method was being used for UIDatePickerView, and thus setting the first component as hidden.
My quick fix was to simply subclass UIPickerView for my special picker in the other file, thus isolating the custom drawRect override there. After I did this, my UIDatePicker displayed properly!

Is there any possible to reset pickerview to start position?

i already put my reloadAllComponents inside didSelectRow of pickerview but it can't reset my pickerview position to start position. is there any possible to reset my pickerview to start position after user selecting data because i use 1 pickerview as an input for many button.
What code should i write and where to write it?
I hope you understand my english. Thank you.
I already put code but it still can't put my pickerView state to start position.
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
year = [picker selectedRowInComponent:0];
month = [picker selectedRowInComponent:1];
day = [picker selectedRowInComponent:2];
date = #"";
if(viewPicker.tag == 1) {
labelTime.text = [date stringByAppendingFormat:#"%d:%d:%d", year , month, day];
else {
...
}
[picker selectRow:0 inComponent:0 animated:YES];
}
reloadAllComponents method just queries your datasource in order to synchronize the data with the picker. If by 'resetting' you mean that you want you picker to move its current selection to the first row you can do something like this:
// Here we select the first row, in the first component with animation
// Customize it accordingly
[myPicker selectRow:0 inComponent:0 animated:YES];
Although be careful. If you call this method in didSelectRow then every time the user selects a row, the picker will go to the first row automatically...

IOS Application Freezes

My application is freezing with a reason that I could not figure out, it does not crash but it freezes.
basically what I did was having another view inside my view that is hidden at first place, after when I click a button this view will be appeared and it includes a UItextfield and a UIPickerView. the pickerview data is updating while I edt the UItextfield and I select one of the values from picker view then basically press addButton, when I add thi whole view will be hidden again and the selected value of the pickerview will be added another UITextfield in the main view.
when I do this first time it works fine. it sets the value of the UI textfield which is on the main view to the correct value. but when I try to change it, get that view unhidden and select another value in the pcikerview and press on addButton again the application freezes. No crash appears.
I want to inform you about this the value of the UITextfield on the main view is changing I checked it on the logs. and the method that do the clicking action completely works. But it freezes.
I couldnt figure it out, I need some help :)
EDIT:
I figure out that the UITextField on the main view cause this freeze, when I set the text
[myTextfield setText:#"somethng"];
the UI freeze. when I comment this out it works pretty fine. But It works fine at first time. does not work on the second time.
Any help will be appreciated.
EDIT 2 (I added some code):
-(IBAction)addButtonClicked:(id)sender
{
leftBarButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStylePlain target:self action:#selector(pickerViewDoneClicked:)];
[[self.navigationController.navigationBar.items objectAtIndex:0] setLeftBarButtonItem:leftBarButton];
[self.pickerContainer setHidden:NO];
}
-(IBAction)pickerViewDoneClicked:(id)sender
{
[[self.navigationController.navigationBar.items objectAtIndex:0] setLeftBarButtonItem:nil];
NSLog(#"%#", self.myTextField);
NSLog(#"old val %#", self.myTextField.text);
// the text field on the main view.
[self.myTextField setText: selectedText];
NSLog(#"new val %#", self.myTextField.text);
[self.pickerContainer setHidden:YES];
}
//in picker view source
selectedText = [pickerData objectAtIndex:row];
As I told the first time I add it works fine I can see the value on the textfield
but when I do the same action again and click done the app freezes.
I checked the reference of the textfield before after and no reference missing
I can see the new value and old value as well. but the UI freezes.
Just to be sure: you do not perform any call to UIKit on non-main thread?
I was using this piece of code for padding for my text field,
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, 20)];
self.myTextField.leftView = paddingView;
self.myTextField.leftViewMode = UITextFieldViewModeAlways;
I am not sure how it effects it but second time I change the text into the textfield I was getting my screen freezed.
Thanks again for any effort to help me.

Cocoa Touch - how to assign value from UIDatePicker to a TextField in a TableView

Hihi all,
I have a fixed row tableView with several TextFields in it for editing purposes. 2 of the fields are for date input. I can call out the datePicker when focus on the 2 fields, but how can I set the selected date into the correspondent TextField?
In my datePickerValueChanged method, how can I know which TextField is currently being edited with the UIDatePicker?
Please help. Great thanks in advance!
:)
you can try this: in .h file take a variable of UITextField *activeTextField now in
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
//Give reference of textField to active one
activeTextField = textField;
return YES;
}
now you can have active text field. Now you can manipulate it in datePickerValueChanged method
Hope it helps you.
You can add a selector to your UIDatePickers as they will have different instance variables.
[datePicker addTarget:self
action:#selector(changeDateInLabel1:)
forControlEvents:UIControlEventValueChanged];
[datePicker2 addTarget:self
action:#selector(changeDateInLabel2:)
forControlEvents:UIControlEventValueChanged];
Then in the method you can do like this :
- (void)changeDateInLabel2:(id)sender{
//Use NSDateFormatter to write out the date in a friendly format
NSDateFormatter *_dateFormatter = [[NSDateFormatter alloc] init];
_dateFormatter.dateStyle = NSDateFormatterMediumStyle;
//Or a text field
label2.text = [NSString stringWithFormat:#"%#",
[_dateFormatter stringFromDate:datePicker2.date]];
[_dateFormatter release];
}
For textField's you can use textFieldShouldBeginEditing or textFieldShouldBeginEditing to check which one you are on.
set the cell which contains the text field as selected. while setting the date, set it in the text filed of the cell which is selected.
this is how Apple prefers doing it. check Date Cell code sample.