diff --git a/OptionSelector.xcodeproj/project.pbxproj b/OptionSelector.xcodeproj/project.pbxproj index 1788504..0291f14 100644 --- a/OptionSelector.xcodeproj/project.pbxproj +++ b/OptionSelector.xcodeproj/project.pbxproj @@ -9,11 +9,14 @@ /* Begin PBXBuildFile section */ 8DECE7921B78082B0064D760 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 8DECE7911B78082B0064D760 /* main.m */; }; 8DECE7951B78082B0064D760 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 8DECE7941B78082B0064D760 /* AppDelegate.m */; }; - 8DECE7981B78082B0064D760 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 8DECE7971B78082B0064D760 /* ViewController.m */; }; 8DECE79B1B78082B0064D760 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 8DECE7991B78082B0064D760 /* Main.storyboard */; }; 8DECE79D1B78082B0064D760 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 8DECE79C1B78082B0064D760 /* Images.xcassets */; }; 8DECE7A01B78082B0064D760 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 8DECE79E1B78082B0064D760 /* LaunchScreen.xib */; }; 8DECE7AC1B78082B0064D760 /* OptionSelectorTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 8DECE7AB1B78082B0064D760 /* OptionSelectorTests.m */; }; + D9B00C2D1B7EAE6A009A2183 /* CategoryModel.m in Sources */ = {isa = PBXBuildFile; fileRef = D9B00C2C1B7EAE6A009A2183 /* CategoryModel.m */; }; + D9EE02801B7AD2B70016AAA7 /* FirstTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = D9EE027F1B7AD2B70016AAA7 /* FirstTableViewController.m */; }; + D9EE02831B7AD4630016AAA7 /* SecondTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = D9EE02821B7AD4630016AAA7 /* SecondTableViewController.m */; }; + D9EE02891B7AD88D0016AAA7 /* CQCategory.m in Sources */ = {isa = PBXBuildFile; fileRef = D9EE02881B7AD88D0016AAA7 /* CQCategory.m */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -32,14 +35,20 @@ 8DECE7911B78082B0064D760 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 8DECE7931B78082B0064D760 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 8DECE7941B78082B0064D760 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; - 8DECE7961B78082B0064D760 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; - 8DECE7971B78082B0064D760 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 8DECE79A1B78082B0064D760 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 8DECE79C1B78082B0064D760 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = ""; }; 8DECE79F1B78082B0064D760 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/LaunchScreen.xib; sourceTree = ""; }; 8DECE7A51B78082B0064D760 /* OptionSelectorTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = OptionSelectorTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 8DECE7AA1B78082B0064D760 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 8DECE7AB1B78082B0064D760 /* OptionSelectorTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OptionSelectorTests.m; sourceTree = ""; }; + D9B00C2B1B7EAE6A009A2183 /* CategoryModel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CategoryModel.h; sourceTree = ""; }; + D9B00C2C1B7EAE6A009A2183 /* CategoryModel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CategoryModel.m; sourceTree = ""; }; + D9EE027E1B7AD2B70016AAA7 /* FirstTableViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FirstTableViewController.h; sourceTree = ""; }; + D9EE027F1B7AD2B70016AAA7 /* FirstTableViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FirstTableViewController.m; sourceTree = ""; }; + D9EE02811B7AD4630016AAA7 /* SecondTableViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SecondTableViewController.h; sourceTree = ""; }; + D9EE02821B7AD4630016AAA7 /* SecondTableViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SecondTableViewController.m; sourceTree = ""; }; + D9EE02871B7AD88D0016AAA7 /* CQCategory.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CQCategory.h; sourceTree = ""; }; + D9EE02881B7AD88D0016AAA7 /* CQCategory.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CQCategory.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -83,8 +92,14 @@ children = ( 8DECE7931B78082B0064D760 /* AppDelegate.h */, 8DECE7941B78082B0064D760 /* AppDelegate.m */, - 8DECE7961B78082B0064D760 /* ViewController.h */, - 8DECE7971B78082B0064D760 /* ViewController.m */, + D9EE02871B7AD88D0016AAA7 /* CQCategory.h */, + D9EE02881B7AD88D0016AAA7 /* CQCategory.m */, + D9B00C2B1B7EAE6A009A2183 /* CategoryModel.h */, + D9B00C2C1B7EAE6A009A2183 /* CategoryModel.m */, + D9EE027E1B7AD2B70016AAA7 /* FirstTableViewController.h */, + D9EE027F1B7AD2B70016AAA7 /* FirstTableViewController.m */, + D9EE02811B7AD4630016AAA7 /* SecondTableViewController.h */, + D9EE02821B7AD4630016AAA7 /* SecondTableViewController.m */, 8DECE7991B78082B0064D760 /* Main.storyboard */, 8DECE79C1B78082B0064D760 /* Images.xcassets */, 8DECE79E1B78082B0064D760 /* LaunchScreen.xib */, @@ -219,9 +234,12 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 8DECE7981B78082B0064D760 /* ViewController.m in Sources */, 8DECE7951B78082B0064D760 /* AppDelegate.m in Sources */, + D9B00C2D1B7EAE6A009A2183 /* CategoryModel.m in Sources */, + D9EE02831B7AD4630016AAA7 /* SecondTableViewController.m in Sources */, + D9EE02891B7AD88D0016AAA7 /* CQCategory.m in Sources */, 8DECE7921B78082B0064D760 /* main.m in Sources */, + D9EE02801B7AD2B70016AAA7 /* FirstTableViewController.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -418,6 +436,7 @@ 8DECE7B11B78082B0064D760 /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; 8DECE7B21B78082B0064D760 /* Build configuration list for PBXNativeTarget "OptionSelectorTests" */ = { isa = XCConfigurationList; @@ -426,6 +445,7 @@ 8DECE7B41B78082B0064D760 /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; diff --git a/OptionSelector/Base.lproj/Main.storyboard b/OptionSelector/Base.lproj/Main.storyboard index d912f9d..d99a9c5 100644 --- a/OptionSelector/Base.lproj/Main.storyboard +++ b/OptionSelector/Base.lproj/Main.storyboard @@ -1,25 +1,96 @@ - + - + - - + + - - - - - - + + - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/OptionSelector/CQCategory.h b/OptionSelector/CQCategory.h new file mode 100644 index 0000000..3470379 --- /dev/null +++ b/OptionSelector/CQCategory.h @@ -0,0 +1,21 @@ +// +// CQCategory.h +// OptionSelector +// +// Created by Xiulan Shi on 8/11/15. +// Copyright (c) 2015 Mike Kavouras. All rights reserved. +// + +#import + +@interface CQCategory : NSObject + +@property (nonatomic) NSString *name; +@property (nonatomic) NSArray *options; +@property (nonatomic) NSString *selection; +@property (nonatomic) NSUInteger index; + +- (void)saveSelection; +- (void)loadDefaults; + +@end diff --git a/OptionSelector/CQCategory.m b/OptionSelector/CQCategory.m new file mode 100644 index 0000000..836f212 --- /dev/null +++ b/OptionSelector/CQCategory.m @@ -0,0 +1,30 @@ +// +// CQCategory.m +// OptionSelector +// +// Created by Xiulan Shi on 8/11/15. +// Copyright (c) 2015 Mike Kavouras. All rights reserved. +// + +#import "CQCategory.h" + +@implementation CQCategory + +- (void)saveSelection { + + // Store the dictionary in NSUserDefaults with the key: self.name + [[NSUserDefaults standardUserDefaults] setObject:self.selection forKey:self.name]; +} + +- (void)loadDefaults { + + NSString *selection = [[NSUserDefaults standardUserDefaults] objectForKey:self.name]; + if (selection != nil) { + self.selection = selection; + } + +} + + + +@end diff --git a/OptionSelector/CategoryModel.h b/OptionSelector/CategoryModel.h new file mode 100644 index 0000000..0321d3c --- /dev/null +++ b/OptionSelector/CategoryModel.h @@ -0,0 +1,19 @@ +// +// CategoryModel.h +// OptionSelector +// +// Created by Xiulan Shi on 8/14/15. +// Copyright (c) 2015 Mike Kavouras. All rights reserved. +// + +#import + +@interface CategoryModel : NSObject + +@property (nonatomic) NSMutableArray *categories; + ++ (instancetype)sharedInstance; + +- (void)initializeModel; + +@end diff --git a/OptionSelector/CategoryModel.m b/OptionSelector/CategoryModel.m new file mode 100644 index 0000000..bc8fadc --- /dev/null +++ b/OptionSelector/CategoryModel.m @@ -0,0 +1,88 @@ +// +// CategoryModel.m +// OptionSelector +// +// Created by Xiulan Shi on 8/14/15. +// Copyright (c) 2015 Mike Kavouras. All rights reserved. +// + +#import "CategoryModel.h" +#import "CQCategory.h" + +@implementation CategoryModel + ++ (instancetype)sharedInstance { + + static CategoryModel *sharedInstance = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + sharedInstance = [[CategoryModel alloc] init]; + }); + return sharedInstance; + +} + + +- (void)initializeModel { + + self.categories = [[NSMutableArray alloc] init]; + + CQCategory *dogs = [[CQCategory alloc] init]; + dogs.name = @"Dogs"; + dogs.options = @[ + @"Labrador Retriever", + @"German Shepherd", + @"Golden Retriever ", + @"Bulldog", + @"Yorksire Terrier", + @"Poodle", + @"Beagle", + @"Chihuahua", + @"Dachshund" + ]; + dogs.selection = @""; + dogs.index = 0; + [self.categories addObject:dogs]; + [dogs loadDefaults]; + + CQCategory *cars = [[CQCategory alloc] init]; + cars.name = @"Cars"; + cars.options = @[ + @"Ford", + @"Honda", + @"Toyota", + @"Mazda", + @"BMW", + @"Audi", + @"Ferari", + @"Porsche", + @"Lamborghini" + ]; + cars.selection = @""; + cars.index = 1; + [self.categories addObject:cars]; + [cars loadDefaults]; + + CQCategory *candies = [[CQCategory alloc] init]; + candies.name = @"Candies"; + candies.options = @[ + @"chocolate bar", + @"chocolate chip", + @"chocolate", + @"lollipop", + @"candy cane", + @"jaw breaker", + @"caramel", + @"sour chew", + @"peanut butter cup", + @"gummi bear" + ]; + candies.selection = @""; + candies.index = 2; + [self.categories addObject:candies]; + [candies loadDefaults]; + +} + + +@end diff --git a/OptionSelector/FirstTableViewController.h b/OptionSelector/FirstTableViewController.h new file mode 100644 index 0000000..df5553a --- /dev/null +++ b/OptionSelector/FirstTableViewController.h @@ -0,0 +1,17 @@ +// +// FirstTableViewController.h +// OptionSelector +// +// Created by Xiulan Shi on 8/11/15. +// Copyright (c) 2015 Mike Kavouras. All rights reserved. +// + +#import +#import "CQCategory.h" +#import "CategoryModel.h" + +@interface FirstTableViewController : UITableViewController + +@property (nonatomic) CategoryModel *model; + +@end diff --git a/OptionSelector/FirstTableViewController.m b/OptionSelector/FirstTableViewController.m new file mode 100644 index 0000000..9003e2e --- /dev/null +++ b/OptionSelector/FirstTableViewController.m @@ -0,0 +1,75 @@ +// +// FirstTableViewController.m +// OptionSelector +// +// Created by Xiulan Shi on 8/11/15. +// Copyright (c) 2015 Mike Kavouras. All rights reserved. +// + +#import "FirstTableViewController.h" +#import "SecondTableViewController.h" + +@interface FirstTableViewController () + +@end + +@implementation FirstTableViewController + + +- (void)viewDidLoad { + [super viewDidLoad]; + + self.model = [CategoryModel sharedInstance]; + [self.model initializeModel]; + +} + +- (void)viewWillAppear:(BOOL)animated { + [super viewWillAppear:animated]; + + [self.tableView reloadData]; +} + +- (void)didReceiveMemoryWarning { + [super didReceiveMemoryWarning]; +} + + +#pragma mark - Table view data source + +- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { + return 1; +} + +- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { + return [self.model.categories count]; +} + + +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { + UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ListPrototypeCell" forIndexPath:indexPath]; + + CQCategory *category = [self.model.categories objectAtIndex:indexPath.row]; + + cell.textLabel.text = category.name; + cell.detailTextLabel.text = category.selection; + [cell layoutIfNeeded]; + return cell; +} + + + +#pragma mark - Navigation + +// In a storyboard-based application, you will often want to do a little preparation before navigation + +- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { + + NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; + CQCategory *currentCategory = [self.model.categories objectAtIndex:indexPath.row]; + SecondTableViewController *detailVC = segue.destinationViewController; + detailVC.category = currentCategory; + +} + +@end diff --git a/OptionSelector/SecondTableViewController.h b/OptionSelector/SecondTableViewController.h new file mode 100644 index 0000000..0bf3c9a --- /dev/null +++ b/OptionSelector/SecondTableViewController.h @@ -0,0 +1,16 @@ +// +// SecondTableViewController.h +// OptionSelector +// +// Created by Xiulan Shi on 8/11/15. +// Copyright (c) 2015 Mike Kavouras. All rights reserved. +// + +#import +#import "CQCategory.h" + +@interface SecondTableViewController : UITableViewController + +@property (nonatomic) CQCategory *category; + +@end diff --git a/OptionSelector/SecondTableViewController.m b/OptionSelector/SecondTableViewController.m new file mode 100644 index 0000000..668b50d --- /dev/null +++ b/OptionSelector/SecondTableViewController.m @@ -0,0 +1,71 @@ +// +// SecondTableViewController.m +// OptionSelector +// +// Created by Xiulan Shi on 8/11/15. +// Copyright (c) 2015 Mike Kavouras. All rights reserved. +// + +#import "SecondTableViewController.h" +#import "CQCategory.h" +#import "CategoryModel.h" + +@interface SecondTableViewController () + +@end + +@implementation SecondTableViewController + +- (void)viewDidLoad { + [super viewDidLoad]; +} + +- (void)didReceiveMemoryWarning { + [super didReceiveMemoryWarning]; +} + +#pragma mark - Table view data source + +- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { + return 1; +} + +- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { + return [self.category.options count]; +} + + +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { + UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OptionCellIdentifier" forIndexPath:indexPath]; + + cell.textLabel.text = [self.category.options objectAtIndex:indexPath.row]; + + return cell; +} + +#pragma mark - Table view delegate + +- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { + + [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; + + NSString *selected = [self.category.options objectAtIndex:indexPath.row]; + + self.category.selection = selected; + + [self.category saveSelection]; +} + + +- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { + + [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone; + +} + + + + + + +@end diff --git a/OptionSelector/ViewController.h b/OptionSelector/ViewController.h deleted file mode 100644 index 6b5159f..0000000 --- a/OptionSelector/ViewController.h +++ /dev/null @@ -1,15 +0,0 @@ -// -// ViewController.h -// OptionSelector -// -// Created by Michael Kavouras on 8/9/15. -// Copyright (c) 2015 Mike Kavouras. All rights reserved. -// - -#import - -@interface ViewController : UIViewController - - -@end - diff --git a/OptionSelector/ViewController.m b/OptionSelector/ViewController.m deleted file mode 100644 index 8c74bbb..0000000 --- a/OptionSelector/ViewController.m +++ /dev/null @@ -1,27 +0,0 @@ -// -// ViewController.m -// OptionSelector -// -// Created by Michael Kavouras on 8/9/15. -// Copyright (c) 2015 Mike Kavouras. All rights reserved. -// - -#import "ViewController.h" - -@interface ViewController () - -@end - -@implementation ViewController - -- (void)viewDidLoad { - [super viewDidLoad]; - // Do any additional setup after loading the view, typically from a nib. -} - -- (void)didReceiveMemoryWarning { - [super didReceiveMemoryWarning]; - // Dispose of any resources that can be recreated. -} - -@end