-
Notifications
You must be signed in to change notification settings - Fork 0
/
HEALViewController.m
85 lines (68 loc) · 2.55 KB
/
HEALViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//
// HEALViewController.m
// UserSettingsTest
//
// Created by Eivind Bakke on 2/20/14.
// Copyright (c) 2014 Halealei. All rights reserved.
//
#import "HEALViewController.h"
@interface HEALViewController ()
@end
@implementation HEALViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[[self weightTextField] setDelegate:self];
[[self sexTextField] setDelegate:self];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
return [textField resignFirstResponder];
}
- (IBAction)updatePersonalDataButton:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSCharacterSet* notDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
if ([[self.weightTextField text] rangeOfCharacterFromSet:notDigits].location == NSNotFound){
NSNumber *weight = [NSNumber numberWithDouble:[[self.weightTextField text] doubleValue]];
[defaults setObject:weight forKey:@"userWeight"];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Invalid Input"
message:@"Please enter a whole number for weight."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
NSString *sex = [self.sexTextField text];
if ([sex isEqualToString:@"M"] || [sex isEqualToString:@"F"]) {
[defaults setObject:sex forKey:@"userSex"];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Invalid Input"
message:@"Please enter F or M for sex."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
@try {
[defaults synchronize];
NSLog(@"Data saved");
}
@catch (NSException *exception) {
NSLog(@"Data save failed.");
}}
@end