I have the following code that sets up a UIDatePicker to show only time. I have two of these UIDatePickers displaying and I want to use them to allow the user to select a time range e.g. between 7 am and 5 pm.
UIDatePicker *datePicker=[[UIDatePicker alloc]init];
datePicker.frame=CGRectMake(-20, 0, 180, 200);
datePicker.datePickerMode = UIDatePickerModeTime;
[datePicker setMinuteInterval:0];
[datePicker setTag:1];
How can I set up the UIDatePicker so that it only shows hours and no minutes?
UIDatePicker isn’t that customizable. You’ll need to set up your own UIPickerView containing the time range you’re looking for. To fit the user’s date/time settings, you should use NSDateComponents and NSDateFormatter to get names for the hours that you need.
This does not take localization into account and will be wrong in practically any other language.
The best advice would be to file a radar at Apple requesting more customization of this API.
Related
I am writing an alarm app. In the app, user can select days from the picker for setting alarm on that day with time. But I don't know to set alarm (UILocalNotification) for specific day in a week (0 - 7).
I just wanted to set fire date of UILocalNotification on specific day like ex. Monday, Friday and Sunday..
I have searched on net and got many similar forums but none of them worked for me.
Any suggestions ?
Set the local notification fireDate for the first notification that should be made. Then set the repeatInterval for the duration between any notification and the next notification.
It would be more helpful if you have included codes which you have tried.
Anyways,
NSDate myOwnDate; // myOwnDate can be a date that is on a specific day. Make sure it is a valid date variable
UILocalNotification* local_notification = [[UILocalNotification alloc] init];
[local_notification setFireDate:myOwnDate];
[local_notification setRepeatInterval:NSWeekCalendarUnit];
[local_notification setAlertBody: #"Your alarm is ringing!"];
UIApplication* app = [UIApplication sharedApplication];
[app scheduleLocalNotification:local_notification];
Pretty straightforward I would say.
I am using the UIDatePicker and I'd like to show month names in Brazilian Portuguese. I've already tried to use the timeZone property, no success though.
Here is my code:
UIDatePicker *pv = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,0,320,216)];
pv.datePickerMode = UIDatePickerModeDate;
pv.timeZone = [NSTimeZone timeZoneWithName:#"America/Sao_Paulo"];
Does anybody would help me?
Set the locale property on the picker to the locale you want, like this:
pv.locale = [[[NSLocale alloc] initWithLocaleIdentifier:#"pt_BR"] autorelease];
It should configure itself correctly. You may need to check [NSLocale availableLocaleIdentifiers] to determine if the locale you want is available/installed.
Clarification needed. Do you only want to set the date picker to Portuguese, and keep the rest of the app in the user's default language?
Normally the datepicker changes language with the user following the settings under settings/general/international. The datepicker will automatically adjust to the user's desired language, so you don't have to localize it directly.
You don't have to do anything. As soon as the iPhone is set to the proper locale, the UIDatePicker will show the proper values for you. That's why locale related methods and properties are deprecated. It relies on system configuration.
I have got a UIDatePicker to pick up Date of Birth. If someone doesn't select a year it defaults to current year. How can I prevent datepicker from defaulting to the current year? Please help.
Thanks
Sonia
It has to default to some year. You could set a maximum date though (like 1 year ago), and then set the date of the UIDatePicker to an invalid date (like today). Then just don't let the user continue if they haven't changed the year to a valid year.
According to The UIDatePicker Class Reference You can simply change the Date Property's value and thus changing the default value - wich is the control's creation date
Regarding your comment about a message - have you tried something like
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Warning" message:#"Please Enter correct birthday year" delegate:self cancelButtonTitle:#"ok" otherButtonTitles:nil];
Alert views are pop-up views that appear over the current view on the iPhone.
You may find iPhone Development 101 - UIAlertView informative
Before showing the date picker, you could call upon the
- (void)setDate:(NSDate *)date animated:(BOOL)animated;
method with the required date and the UIDatePicker will display that date when it comes on screen.
In my application i want to textField where user can put date. Now for taking date i need calendar so that user select date from it and it automatically fill into textField.
What would be the best approach to do so.
Thanks
UIDatePicker will allow the user to easily select the date.
uidatepicker and take the value changed
I built a fancy UITextField class that set up a UIDatePicker as its inputView. The main value to this is that, when the user taps into the text field, the date picker is brought up where the keyboard would normally be.
UIDatePicker *input = [[UIDatePicker alloc] init];
[input addTarget:self action:#selector(update:) forControlEvents:UIControlEventValueChanged];
(UITextField *)myTextField.inputView = input;
[input release];
Then, in the -[update:] method, use an NSDateFormatter to set the text in the textfield.
I am new to iPhone.
I need to display UIPickerview in UIAlertView and I need to get UIPickerview selected value.
Can anyone please post some code or link?
I will not do it.
I am afraid it is not possible to 'pack' UIPickerView into UIAlertView. UIPickerView seems to be always AS wide AS the whole screen (at least on the iPhone) - so even if you manage to add the picker as a subview, it will cover the alert.
I think it will spoil Apple Human Interface Guidelines.
Why do you insist to present a picker in alert? Can't you present it alone, without alert?
I would strongly recommend not doing that. Use the picker view as it's intended—as it's used in Clock and Calendar, i.e. at the bottom of the screen—and you're much less likely to get your app rejected by Apple for violating their Human Interface Guidelines.
As for the alert, you could create something yourself that looks similar, but I don't see why you'd want to combine a alert and a datePicker.
To access the datePicker's date, you need to have it declared in your code. Then you can simply use this:
NSDate *date = self.datePicker.date;
To get the date from a datePicker named datePicker.
To get this date into a string, which you probably want, do this:
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"dd MMM, yyyy"];
NSString *stringFromDate = [formatter stringFromDate:date];