Is there any possible to reset pickerview to start position? - iphone

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...

Related

How to set selection indicator on passing object Index in picker View

I have 3 buttons on one View of UIViewController. On each button click event i called same picker view with different object values.
how to set the selection indicator on last object index for particular
button.?
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated
Selects a row in a specified component of the picker view.
Source
In button click event you can make use of this.
selectRow:inComponent:animated: - Helps you select particular row in the picker.
numberOfRowsInComponent: - Returns the count of rows in the particluar component.
So inorder to select the last object index, you can make use of this.
[self.pickerView selectRow:[self.pickerView numberOfRowsInComponent:0] inComponent:0 animated:NO];
You can do this using following snippet
[self.pickerView selectRow:MAX_VALUE inComponent:0 animated:NO];
where MAX_VALUE = total no. of rows in component - 1;
Enjoy Programming !!

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.

How to change the picker rows based on another picker's selection?

I have two pickers - country and region. I need to populate the region picker based on the row selected in the country picker. I have arrays to populate both the pickers. I need some help to change the rows of region picker based on the country picker's selection. Any help would be greatly appreciated. Thanks a lot in advance.
Ok You have two pickers lets say countryPicker and regionPicker.
in the delegate method for UIPickerView add one condition
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *pickerTitle = #"";
if (pickerView == countryPicker)
{
pickerTitle = [countryFeeds objectAtindex:row];
//assigns the country title if pickerView is countryPicker
}
else if (pickerView == regionPicker)
{
pickerTitle = [regionFeeds objectAtindex:row];
//assigns the region title if pickerView is regionPicker
}
return pickerTitle;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (pickerView == countryPicker)
{
//selects the region corresponding to the selected country.
//totalRegions is a NSDictionary having country names as keys and array of their regions as values
regionFeeds = [totalRegions objectForKey:[countryFeeds objectAtindex:row]];
//Now reloading the regionPicker with new values.
[regionPicker reloadAllComponents];
}
else if (pickerView == regionPicker)
{
// your code to select a region
}
}
I hope this solves your problem :)
BR, Hari
Are they separate picker views, or one picker view with two columns?
Either way, when you change the value in one picker, just call reloadAllComponents on the other picker (or reloadComponent: if you are using a split picker) and then when it reloads the data from it's data source you can use the value from the first picker to supply the correct region list.
My suggestion is please maintain the country and region in one picker with two components(country,region).If u do like this then we can change the one component values based on another component value.
NSUInteger selectedRow= [CountryPicker selectedRowInComponent:0];
NSString *userchoice =[self pickerView:CountryPicker titleForRow:selectedRow forComponent:0];
This will give you the title for the selected row by the user in country picker
then using the arrays you have to populate names of regions
You can use
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated
on the RegionPicker

label not updating in setter

ok so this problem is kinda weird because the NSLog I have right in front of the line of code that should be printing out the text is returning the correct value.
Here's the code:
-(void)setCurrentDate:(UILabel *)currentDate
{
NSInteger onDay = 1; //because if it's today, you are on day one, not zero... no such thing as a day zero
//get the nubmer of days left
if( [[NSUserDefaults standardUserDefaults] objectForKey:#"StartDate"] ){ //if there is something at the userdefaults
onDay = [self daysToDate:[NSDate date]];
}//otherwise, onDay will just be one
self.theCurrentNumberOfDaysSinceStart = onDay;
NSLog(#"On day: %d", onDay); //this is returning the correct values....
//print it out on the label
[currentDate setText:[NSString stringWithFormat:#"On day: %d", onDay]];//echoes out the current day number
}
So when the app first launches, everything is fine. The label updates and everything. The problem arises when I hit a button that basically grabs a new date. In the process, it runs this:
//need to reload the "on day" label now
[self setCurrentDate:self.currentDate];
//and the "days left" label
[self setDaysLeft:self.daysLeft];
Again, I'm thinking this should all be correct because the NSLog is returning the correct stuff. I'm thinking that the problem is with the last line in the first block of code I showed... the line with the setText.
thanks for all your help!
cheers,
Matt
If you used a nib
When the nib loads and establishes all of it connections it... (From the Resource Programming guide)
looks for a method of the form set OutletName: and calls it if such a method is present
Therefore the nib will load and call setCurrentDate: passing in the unarchived UILabel as the parameter
In your method you configure the UILabel using the local reference passed into the method
[currentDate setText:[NSString stringWithFormat:#"On day: %d", onDay]];
You at no point actually store a reference to this UILabel in an ivar, so technically you have leaked the label and as you have not set the ivar currentDate it will be initialised to nil. This is the danger of overriding a setter with an incorrect implementation.
At some point in your method you should be setting your ivar to the passed in variable. A normal setter would look like this
- (void)setCurrentDate:(UILabel *)currentDate;
{
if (_currentDate != currentDate) {
[_currentDate release];
_currentDate = [currentDate retain];
}
}
But
In your example I would not worry about this at all I would instead change this
//need to reload the "on day" label now
[self setCurrentDate:self.currentDate];
to something like
[self updateCurrentDate];
The implementation would look something like:
- (void)updateCurrentDate;
{
NSInteger onDay = 1;
if ([[NSUserDefaults standardUserDefaults] objectForKey:#"StartDate"]) {
onDay = [self daysToDate:[NSDate date]];
}
self.theCurrentNumberOfDaysSinceStart = onDay;
[self.currentDate setText:[NSString stringWithFormat:#"On day: %d", onDay]];
}

How fixed tag value for text field of custom cell in table view?

I am implementing a tab;e in which i have created a custom cell for cell of tableview. On custom cell i am displaying two text field , one button and one image view. Now i want implement an event on text field. That is when i click on text field then appear picker view. With the help of picker view we can change text of text field. I have applied event like as when we click on text filed then appear a picker view. But when i scroll picker view then text field value not changing. so what i will do so that it happens?
In table view i have 10 rows and i want to insert value from single picker view. I have also set tag on text field of custom cell by using this code.
cell_object.txt_time.tag=[indexpath.row];
for selection value from picker view i use this code...
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{
if (pickerView == myPicker)
{
if(cell_object.txt_time.tag==0)
{
[cell_object.txt_time setText:[NSString stringWithFormat:#"%#",[array_time objectAtIndex: [pickerView selectedRowInComponent:0]]]];
}
else if(cell_object.txt_time.tag==1)
{
[cell_object.txt_time setText:[NSString stringWithFormat:#"%#",[array_time objectAtIndex: [pickerView selectedRowInComponent:0]]]];
}
}
}
I have do for 10 rows text.tag. But it is not working.
What i do ?
Thanks in advance...
A simple implementation would be:
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent(NSInteger)component{
NSString *string = [[NSString alloc] initWithFormat:#"%#",[myArray objectAtIndex:row]];
textfield.text = string;
}
myArray is the datasource for your UIPickerView. With the above code you will be able to tweak it to what you needed to do.