-1

I want the user to enter their time, for example they can enter 4:30 and I'll just get the 4 and 30. or is there a easier way to get the time from the textfield? I know how to get a integer from the textfield but I want to know how to get two or the time, whichever one is easier.

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([[segue identifier]isEqualToString:@"submitsegue"])
    {
        secondViewController *vc2 = [segue destinationViewController];
        vc2.inputfrom1 = [age.text integerValue];
    }
}

Thats my code to get a number.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • A textfield just holds a string. It is up to you to parse the input. You can use NSString methods to do this. You can also use `UITextField` delegate methods to validate characters as the user enters data, but you may be better off looking at a different type of user interface object, such as a pickerView – Paulw11 Mar 25 '14 at 22:57
  • Where's your code to parse the time field? – rmaddy Mar 25 '14 at 23:06
  • is the picker view a better option for the user putting time? – edwingomez Mar 25 '14 at 23:09
  • And I don't know how to parse the time field. – edwingomez Mar 25 '14 at 23:13
  • Do you want the current time in the text field? – JSA986 Mar 25 '14 at 23:52
  • If you simply want the hours and minutes then all you need to do is split the string on the colon. See the docs for `NSString`. – rmaddy Mar 26 '14 at 00:21

1 Answers1

1

You could convert your string to a date and then store the components of that date:

NSString *dateString = self.textField.text;

// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"HH:mm"]; //this is the format for 24-hour
NSDate *date = [dateFormat dateFromString:dateStr];

(the original answer to the top part: Convert string to date in my iPhone app)

Then you extract the components and save them as hours and minutes:

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:date];
int hour = [components hour];
int minute = [components minute];

(original answer: How do I get hour and minutes from NSDate?)

If your string isn't a string that's of valid format, your NSDate object will be nil, so you could validate it there.

If a date picker meets your criteria as well, you could just use that and not display the date component. (how to display only time in UIDatePicker iphone)

Community
  • 1
  • 1
jancakes
  • 456
  • 4
  • 10
  • Since the OP just wants the hours and minutes as numbers, it would be simpler and much more efficient to simply split the string on the colon and convert the two results to integers. Simply validation can check they are in range. – rmaddy Mar 26 '14 at 00:22
  • Yes, that is another alternative. My approach does get the OP the hours and minutes as numbers, and, in my opinion simplifies the validation, as date will come out nil for invalid input. If OP just splits the string, then each component will need to be checked for validity, for length, for negative vs positive, etc. – jancakes Mar 26 '14 at 00:28