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!
Related
I am trying to implement a suggestion offered by already answered question, but I am stuck on getting it to work, so looking for some help.
Here is the post I am trying to implement: http://stackoverflow.com/a/3067579/589310
I am trying to use the solution offered by "ridale" on how to make a "detailLabel" editable as part of a TableView. I hope it will allow me to directly edit a cell and enter a number. It doesn't seem too common as a UI, but "SmartRecord" does it and I want to emulate it.
Here is the only line that gives me an error:
UITextField *tmpView = [self detailLabel:indexPath];
I get this error:
Instance method -detailLabel: not found (return type defaults to 'id')
I assume it is because my self is different than the original poster.
I added a TableView directly to my existing controller. It is not a TableViewController directly:
#interface EditViewController : UIViewController
{
IBOutlet UITableView *tableSettings;
}
I can fill the table and interact with it, so I know it works (at some level anyway).
I have tried changing self to my table control or the cell directly:
UITextField *tmpView = [tableSettings detailLabel:indexPath];
I can't find anything that responds to the "detailLabel" method.
I am also not sure if the proposed solution is complete or uses more code not shown.
This is the only error I get, so I am hopeful once I solve it, it will work ;-)
-(UITextField*)detailLabel:(NSIndexPath*)indexPath is not present in UITableView or any other classes provided by Apple.
The frame position is just for sample. Modify that such that it would come in place of your detailLabel.
You have to write your own method which returns a UITextField, something as following
-(UITextField*)detailLabel:(NSIndexPath*)indexPath
{
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
textField.delegate = self;
//assuming myList is array of NSString
NSString* str = [myList objectAtIndex:indexPath.row];
textField.text = str;
return [textField autorelease];
}
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.
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.
Hey
Just wondering how do you..
Iv created some code that automatically creates a certain amount of UITextField input by the user.
Each UITextfield has a set tag automatically created.
The user then inputs his value into each of the UITextFields.
I want to retrieve the input text from the UITextFields which correspond to the tag.
I thought I nearly had it with:
NSString * tmpV = (NSString*)[choiceTextField.text viewWithTag:result];
where result is a increment, choiceTextField is the UITextField. With this I get a problem not defining instance, which I can't do as the UITextFields are generated in code not on the xib.
So to sum up, basically want the retrieve the text from a UITextField with specific tag.
Thanks
Dan
UITextField *yourTextField = (UITextField *)[self.view viewWithTag:result];
NSString *getText = myTextField.text;
Hope this helps
Your textfields must be a subview of a parent view. You need to write the following in your view controller of the parent view:
NSString *text = ((UITextField*)[self.view viewWithTag:result]).text;
Just go with Hetal Vora's solution: looks much cleaner!
-viewWithTag can be called on any view and searches all its subviews for one that matches the tag. Thus, you will find your UITextField by calling it on any view that is above it in the hierarchy. For example, you could call it on the view controller's view that contains your text field, as follows:
NSString *tmpV = [[myViewController.view viewWithTag:result] text];
If you are already in the view controller's class you could use:
NSString *tmpV = [[self.view viewWithTag:result] text];
On second thought, the following may be more correct & will avoid any compiler errors:
UITextField *myTextField = (UITextField *)[self.view viewWithTag:result];
NSString *tmpV = myTextField.text;
I have a UITableView with cells that contain a UISwitch control. It's similar to the table view in the iPhone's Clock app shown below...
(source: epicself.com)
In my app's cellForRowAtIndexPath method, I create and attach the UISwitch control like so...
CGRect frameSwitch = CGRectMake(215.0, 10.0, 94.0, 27.0);
UISwitch *switchEnabled = [[UISwitch alloc] initWithFrame:frameSwitch];
[switchEnabled addTarget:self action:#selector(switchToggled:) forControlEvents:UIControlEventValueChanged];
cell.accessoryView = switchEnabled;
My question is, when the switch is toggled by the user and the switchToggled method is called, how can I tell which table cell it belongs to? I can't really do much with it without knowing it's context.
Thanks so much in advance for your help!
In your action method, you can cast the superview of the passed-in UISwitch (which is the UITableViewCell itself), then pass that to the tableView's indexPathForCell method.
indexPathForCell will return a NSIndexPath object, which you can then use to index to your datamodel to modify. Then all you gotta do is call reloadData on your tableView.
Also, in cellForRowAtIndexPath, you should set the UISwitch's state based on your model.
First of all, fix the memory leak:
UISwitch *switchEnabled = [[[UISwitch alloc] initWithFrame:frameSwitch] autorelease];
Then pick one of these options:
switchEnabled.tag = someInt; // before adding it to the cell
NSLog(#"switched %d",switch.tag); // to see which switch was toggled
or
UITableViewCell *parentCell = switchEnabled.superview;
// + some magic to see which cell this actually is