I'm using a UIDatePicker in UIDatePickerModeCountDownTimer mode which lets the user select hours and minutes, but it won't let the user select zero hours and zero minutes.
If the hours are set to zero and the user tries to pick zero minutes, it automatically jumps to 01.
I've researched the docs and nothing seems to allow me to do that, short of creating a custom picker.
The problem with creating a custom picker is that I lose the title in the selection indicator (hours, minutes) and there doesn't seem to be a way of adding those either.
I've been looking for an answer for the past 2 days! I can't believe UIPickerView doesn't have a property to set the title for indicators for each section.
Any help would be appreciated.
If you do choose to create a custom UIPicker you can use the titleForRow method to change the labels in each component (such as "hours", "minutes", etc).
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if(component == 0){
return [NSString stringWithFormat:#"Hours"];
}else if (component == 1){
return [NSString stringWithFormat:#"Minutes"];
}
}
I suppose a count-down timer is not intended for not counting down. So, you have at least one minute. If you do not want a timer at all, you need an additional switch for enabling the count-down.
BTW., I stumbled over a kind of an opposite problem. I set the interval to 5 minutes. Then, I can select 0 minutes. I wanted to know how to prevent a user from setting 0. In fact, in my case I get also 1 minute, only the picker shows 0.
So, you could use this to let the user choose 0 in the picker and then interpret the 1 minute as 0 in your code. But I wouldn't do this, since the behavior of the picker might change in the future. I consider the current behavior a bug.
Related
i have a functionility for show count down timer in format, but when uidatepicker in count down timer mode , it show only HH:MM.
how i can add a extra field for second in uidatepickerview.
how i can add a extra field for second in uidatepickerview.
You can't. UIDatePicker only displays hours and minutes in countdown mode. If you need to display seconds as well, you'll have to recreate it yourself using a UIPickerView.
As always, you should file an enhancement request to request this functionality.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 2;
}
it will create section in your picker view.
Ok so I have a segmentedControl which I need to dynamically remove and add segments to. This piece of code will remove the first segment but then doesnt set the correct segment to be selected. The strange thing is when I print it out all the numbers are correct it is just the highlighted segement on the screen which is always out by -1.
if ([outControl numberOfSegments]==4) {
int previous = [outControl selectedSegmentIndex];
if (previous>0) {
previous--;
}
[outControl removeSegmentAtIndex:0 animated:YES];
NSLog(#"setting to %d with %d segments", previous ,[outControl numberOfSegments]);
[outControl setSelectedSegmentIndex:previous];
}
Here is the log setting to 1 with 3 segments. With this log segment 0 is actually selected in the control and I cannot press segment 1 as the control thinks it is selected?
I can press segment 0 though even though that is the one being displayed as selected.
Here is a screen grab to try and clarify my issue.
image
The text in the cell is correct as cell 1 is actually selected as you can see by the log. But the highlighted cell is 0!? Its driving me crazy. It all works when adding segments its only when removing I get the issue.
Can anyone see where its going wrong??
Many thanks
Jules
I have noticed that even if i do not set the new selected segment I get the same results ie the wrong segment is selected.....?? Also this is all coded with cellForRowAtIndexPath if that helps anyone?
The code looks right yet behaves (wrongly) just as you described. It seems that the control gets in an awkward state when changing the selection in the same turn of the run loop as the segment deletion. But this works:
-(void)setSegmentedSelection:(NSNumber *)indexNum {
NSInteger index = [indexNum intValue];
[outControl setSelectedSegmentIndex:index];
}
// then instead of setting the selected index after the segment is removed,
// let the run loop return...
[self performSelector:#selector(setSegmentedSelection:) withObject:[NSNumber numberWithInt:previous] afterDelay:0.0];
I'm surprised that it doesn't work as you coded, but at least here's a workaround until we figure it out.
I am trying to make a picker appear to spin around multiple times before landing on a particular row. I have tried using for and do while loops. I have even tried calling multiple methods in succession, but each time, the picker only appears to reload once even though I am telling it to reload multiple times before finally landing on a selected row like this:
int newValue = random() % [self.inputNames count];
[payPicker selectRow:newValue inComponent:0 animated:YES];
[payPicker reloadComponent:0];
NSDate *future = [NSDate dateWithTimeIntervalSinceNow: 2.0 ];
[NSThread sleepUntilDate:future];
newValue = random() % [self.inputNames count];
[payPicker selectRow:newValue inComponent:0 animated:YES];
[payPicker reloadComponent:0];
[NSThread sleepUntilDate:future];
I do the above about three or four times and each time the picker only appears to reload once and goes to the final selected row. Is there something special I have to do or is it possible at all to make the picker appear to spin around and around? If I could even get it to jump randomly between the row choices before landing on one, I would be fine with that.
Thanks!!
Don't sleep the UI thread. No animation will start until you exit this method, after all your sleeping. So return from the method with the first request for the picker to animate. Do subsequent picker animation requests in timer callbacks after the previous picker animation has finished. Rinse and repeat as necessary.
The way other apps do this is by not using a real picker at all, but overlaying the picker control with an animated image sequence which just happens to look like a picker spinning. Then remove the fake animation to show an identical looking but real picker underneath. The advantage of your own animated cartoon is that you can make the fake picker spin in ways that a real picker can't.
Perhaps you could try selecting random rows before selecting your actual row. For example, if you want to select row 0, select rows in the following order - 9, 2, 15, 0. I don't think picker view will rotate if it is already on the row it is supposed to go to.
Also, you should select the rows every time after your pickerView:didSelectRow:inComponent: method hits.
Any recommendations re which approach from a useability perspective for a user to configure one preference for my app, which is number of weeks - probably would make sense to allow it to be anywhere from 2 through to 25 say. So options which come to mind include:
type it in - but would need some validation
have a list to pick from (I assume by pushing on another TableView to the UINavigationStack) - but with 25 rows not sure about this (e.g. row 1 = "week 1", row 2 = "week 2" etc)
UIPicker wheel - but doesn't sound too good
other???
thanks
I would advice you to us a UIPickerView that shows when you tap to edit
Is there a way to have a UIPickerView just have escalating numbers to infinity?
Haha. Just kidding, but seriously, I want just a bunch of numbers going up, but I don't want to have to hard-code it all.
Thanks in advance!
I'm pretty sure this can be done with some cleverness (a custom integer class that supports arbitrarily large numbers, and periodically "rolling over" to a higher set of numbers once the user has scrolled through all NSIntegerMax rows (i.e. when the user scrolls to row NSIntegerMax you programmatically scroll them back to row 0 and display "row + NSIntegerMax" instead of just "row"). As they scroll through repeatedly you change it to "row + NSIntegerMax * 2", etc.
Obviously you also have to handle them changing directions are some point.
But I suspect that's what fancier than what you're looking to do. In this case you just want to add these lines of code to your View Controller:
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return NSIntegerMax;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [NSString stringWithFormat:#"%d", row];
}
Then double click on the View nib file (the *.xib file) to open it in Interface Builder, and connect the "data source" outlet to the "File Owner" (the View Controller). That will get you as many integers as I could ever imagine someone scrolling to.
Have a look at this question. It is a variation on your problem.
You could just use a UIStepper. I know you asked about UIPicker but do you really need to use that class or were you using it to explain what kind of thing you wanted