LOGIC & INTERFACE 
Building our App
OVERVIEW 
• Lesson 1: Introductions 
• Lesson 2: iOS specifics 
• Lesson 3: Data Model 
• Lesson 4: Logic (Controller) & Interface
LESSON 3: DATA MODEL 
• Hour 1: Storyboard 
• Hour 2: Creating & Editing 
• Hour 3: Display & Deleting Notes
Storyboard
Storyboard
Storyboard 
• Visual representation of iOS user interface (UI) 
• Shows screens of content and connections 
between screens. Screens referred to as 
“Scene” 
• 1 “Scene” represents 1 View Controller and 
Views 
• Many “views” can be placed on 1 “scene” (e.g. 
buttons, table views, text views). Think of views
Storyboard 
• Each scene has a dock (displays icons 
representing the top-level objects of the 
scene)
Storyboard 
• The “dock” is where we make connections 
between code in our View Controller and its 
Views (“visual objects on the scene”)
View Controllers 
• A storyboard displays View Controllers and 
corresponding Views visually
View Controllers 
• A storyboard displays View Controllers and 
corresponding Views visually
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
// Override point for customization after application launch. 
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { 
UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController; 
UINavigationController *navigationController = [splitViewController.viewControllers lastObject]; 
splitViewController.delegate = (id)navigationController.topViewController; 
} 
return YES; 
} 
View Controllers
View Controllers
View Controllers 
UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
View Controllers 
UINavigationController 
• Sub-class of UIViewController 
• a special View Controller that manages the navigation of 
hierarchical content
View Controllers 
UINavigationController 
• It is a “container” that embeds content of other View 
Controllers inside itself
Creating and Editing
View Controllers 
AppDelegate.m 
#import "AppDelegate.h" 
#import "Data.h" 
@implementation AppDelegate 
- (BOOL)application:(UIApplication *)application 
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
[Data getAllNotes]; 
return YES; 
}
View Controllers 
MasterViewController.m 
#import “Data.h" 
- (void)insertNewObject:(id)sender 
{ 
if (!_objects) { 
_objects = [[NSMutableArray alloc] init]; 
} 
//[_objects insertObject:[NSDate date] atIndex:0]; 
NSString *key = [[NSDate date] description]; 
[Data setNote:kDefaultText forKey:key]; 
[Data setCurrentKey:key]; 
[_objects insertObject:key atIndex:0]; 
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
}
View Controllers 
DetailViewController.m 
#import “Data.h" 
- (void)setDetailItem:(id)newDetailItem 
{ 
if (_detailItem != newDetailItem) { 
_detailItem = newDetailItem; 
[Data setCurrentKey:_detailItem]; 
// Update the view. 
[self configureView]; 
} 
if (self.masterPopoverController != nil) { 
[self.masterPopoverController dismissPopoverAnimated:YES]; 
} 
}
View Controllers 
DetailViewController.m 
- (void)configureView 
{ 
NSString *currentNote = [[Data getAllNotes] objectForKey:[Data getCurrentKey]]; 
if (![currentNote isEqualToString:kDefaultText]) { 
self.tView.text = currentNote; 
} else { 
self.tView.text = @""; 
} 
[self.tView becomeFirstResponder]; 
}
View Controllers 
DetailViewController.m 
- (void)viewWillDisappear:(BOOL)animated 
{ 
if (![self.tView.text isEqualToString:@""]) { 
[Data setNoteForCurrentKey:self.tView.text]; 
} else { 
[Data removeNoteForKey:[Data getCurrentKey]]; 
} 
[Data saveNotes]; 
}
Displaying & Deleting
View Controllers 
MasterViewController.m 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath 
*)indexPath 
{ 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" 
forIndexPath:indexPath]; 
NSDate *object = _objects[indexPath.row]; 
cell.textLabel.text = [[Data getAllNotes] objectForKey:[object description]]; 
return cell; 
}
View Controllers 
MasterViewController.m 
- (void)makeObjects 
{ 
_objects = [NSMutableArray arrayWithArray:[[Data getAllNotes] allKeys]]; 
[_objects sortUsingComparator:^NSComparisonResult(id obj1, id obj2) { 
return [(NSDate *)obj2 compare:(NSDate *)obj1]; 
}]; 
}
View Controllers 
MasterViewController.m 
- (void)insertNewObject:(id)sender 
{ 
[self makeObjects]; 
if (!_objects) { 
_objects = [[NSMutableArray alloc] init]; 
} 
//[_objects insertObject:[NSDate date] atIndex:0]; 
NSString *key = [[NSDate date] description]; 
[Data setNote:kDefaultText forKey:key]; 
[Data setCurrentKey:key]; 
[_objects insertObject:key atIndex:0]; 
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
[self performSegueWithIdentifier:kDetailView sender:self]; 
}
View Controllers 
MasterViewController.m
View Controllers 
MasterViewController.m 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { 
NSDate *object = _objects[indexPath.row]; 
self.detailViewController.detailItem = object; 
} 
} 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
if ([[segue identifier] isEqualToString:@"showDetail"]) { 
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
NSDate *object = _objects[indexPath.row]; 
[[segue destinationViewController] setDetailItem:object]; 
} 
}
View Controllers 
MasterViewController.m 
- (void)viewWillAppear:(BOOL)animated 
{ 
[super viewDidAppear:animated]; 
[self makeObjects]; 
[self.tableView reloadData]; 
}
View Controllers 
MasterViewController.m 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
if (editingStyle == UITableViewCellEditingStyleDelete) { 
[Data removeNoteForKey:[_objects objectAtIndex:indexPath.row]]; 
[Data saveNotes]; 
[_objects removeObjectAtIndex:indexPath.row]; 
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
} else if (editingStyle == UITableViewCellEditingStyleInsert) { 
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table 
view. 
} 
}
OVERVIEW 
• Lesson 1: Introductions 
• Lesson 2: iOS specifics 
• Lesson 3: Data Model 
• Lesson 4: Logic (Controller) & Interface

iOS Beginners Lesson 4

  • 1.
    LOGIC & INTERFACE Building our App
  • 2.
    OVERVIEW • Lesson1: Introductions • Lesson 2: iOS specifics • Lesson 3: Data Model • Lesson 4: Logic (Controller) & Interface
  • 3.
    LESSON 3: DATAMODEL • Hour 1: Storyboard • Hour 2: Creating & Editing • Hour 3: Display & Deleting Notes
  • 4.
  • 5.
  • 6.
    Storyboard • Visualrepresentation of iOS user interface (UI) • Shows screens of content and connections between screens. Screens referred to as “Scene” • 1 “Scene” represents 1 View Controller and Views • Many “views” can be placed on 1 “scene” (e.g. buttons, table views, text views). Think of views
  • 7.
    Storyboard • Eachscene has a dock (displays icons representing the top-level objects of the scene)
  • 8.
    Storyboard • The“dock” is where we make connections between code in our View Controller and its Views (“visual objects on the scene”)
  • 9.
    View Controllers •A storyboard displays View Controllers and corresponding Views visually
  • 10.
    View Controllers •A storyboard displays View Controllers and corresponding Views visually
  • 11.
    - (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController; UINavigationController *navigationController = [splitViewController.viewControllers lastObject]; splitViewController.delegate = (id)navigationController.topViewController; } return YES; } View Controllers
  • 12.
  • 13.
    View Controllers UISplitViewController*splitViewController = (UISplitViewController *)self.window.rootViewController;
  • 14.
    View Controllers UINavigationController • Sub-class of UIViewController • a special View Controller that manages the navigation of hierarchical content
  • 15.
    View Controllers UINavigationController • It is a “container” that embeds content of other View Controllers inside itself
  • 16.
  • 17.
    View Controllers AppDelegate.m #import "AppDelegate.h" #import "Data.h" @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [Data getAllNotes]; return YES; }
  • 18.
    View Controllers MasterViewController.m #import “Data.h" - (void)insertNewObject:(id)sender { if (!_objects) { _objects = [[NSMutableArray alloc] init]; } //[_objects insertObject:[NSDate date] atIndex:0]; NSString *key = [[NSDate date] description]; [Data setNote:kDefaultText forKey:key]; [Data setCurrentKey:key]; [_objects insertObject:key atIndex:0]; NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; }
  • 19.
    View Controllers DetailViewController.m #import “Data.h" - (void)setDetailItem:(id)newDetailItem { if (_detailItem != newDetailItem) { _detailItem = newDetailItem; [Data setCurrentKey:_detailItem]; // Update the view. [self configureView]; } if (self.masterPopoverController != nil) { [self.masterPopoverController dismissPopoverAnimated:YES]; } }
  • 20.
    View Controllers DetailViewController.m - (void)configureView { NSString *currentNote = [[Data getAllNotes] objectForKey:[Data getCurrentKey]]; if (![currentNote isEqualToString:kDefaultText]) { self.tView.text = currentNote; } else { self.tView.text = @""; } [self.tView becomeFirstResponder]; }
  • 21.
    View Controllers DetailViewController.m - (void)viewWillDisappear:(BOOL)animated { if (![self.tView.text isEqualToString:@""]) { [Data setNoteForCurrentKey:self.tView.text]; } else { [Data removeNoteForKey:[Data getCurrentKey]]; } [Data saveNotes]; }
  • 22.
  • 23.
    View Controllers MasterViewController.m - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; NSDate *object = _objects[indexPath.row]; cell.textLabel.text = [[Data getAllNotes] objectForKey:[object description]]; return cell; }
  • 24.
    View Controllers MasterViewController.m - (void)makeObjects { _objects = [NSMutableArray arrayWithArray:[[Data getAllNotes] allKeys]]; [_objects sortUsingComparator:^NSComparisonResult(id obj1, id obj2) { return [(NSDate *)obj2 compare:(NSDate *)obj1]; }]; }
  • 25.
    View Controllers MasterViewController.m - (void)insertNewObject:(id)sender { [self makeObjects]; if (!_objects) { _objects = [[NSMutableArray alloc] init]; } //[_objects insertObject:[NSDate date] atIndex:0]; NSString *key = [[NSDate date] description]; [Data setNote:kDefaultText forKey:key]; [Data setCurrentKey:key]; [_objects insertObject:key atIndex:0]; NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; [self performSegueWithIdentifier:kDetailView sender:self]; }
  • 26.
  • 27.
    View Controllers MasterViewController.m - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { NSDate *object = _objects[indexPath.row]; self.detailViewController.detailItem = object; } } - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString:@"showDetail"]) { NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; NSDate *object = _objects[indexPath.row]; [[segue destinationViewController] setDetailItem:object]; } }
  • 28.
    View Controllers MasterViewController.m - (void)viewWillAppear:(BOOL)animated { [super viewDidAppear:animated]; [self makeObjects]; [self.tableView reloadData]; }
  • 29.
    View Controllers MasterViewController.m - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { [Data removeNoteForKey:[_objects objectAtIndex:indexPath.row]]; [Data saveNotes]; [_objects removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; } else if (editingStyle == UITableViewCellEditingStyleInsert) { // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view. } }
  • 30.
    OVERVIEW • Lesson1: Introductions • Lesson 2: iOS specifics • Lesson 3: Data Model • Lesson 4: Logic (Controller) & Interface