iOS Beginners Lesson 4

30
LOGIC & INTERFACE Building our App

description

iOS Beginners Lesson 4

Transcript of iOS Beginners Lesson 4

Page 1: iOS Beginners Lesson 4

LOGIC & INTERFACEBuilding our App

Page 2: iOS Beginners Lesson 4

OVERVIEW

• Lesson 1: Introductions

• Lesson 2: iOS specifics

• Lesson 3: Data Model

• Lesson 4: Logic (Controller) & Interface

Page 3: iOS Beginners Lesson 4

LESSON 3: DATA MODEL

• Hour 1: Storyboard

• Hour 2: Creating & Editing

• Hour 3: Display & Deleting Notes

Page 4: iOS Beginners Lesson 4

Storyboard

Page 5: iOS Beginners Lesson 4

Storyboard

Page 6: iOS Beginners Lesson 4

• 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 as “visual elements” or simply as “objects”

Storyboard

Page 7: iOS Beginners Lesson 4

• Each scene has a dock (displays icons representing the top-level objects of the scene)

Storyboard

Page 8: iOS Beginners Lesson 4

• The “dock” is where we make connections between code in our View Controller and its Views (“visual objects on the scene”)

Storyboard

Page 9: iOS Beginners Lesson 4

• A storyboard displays View Controllers and corresponding Views visually

View Controllers

Page 10: iOS Beginners Lesson 4

• A storyboard displays View Controllers and corresponding Views visually

View Controllers

Page 11: iOS Beginners Lesson 4

- (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

Page 12: iOS Beginners Lesson 4

View Controllers

Page 13: iOS Beginners Lesson 4

View ControllersUISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;

Page 14: iOS Beginners Lesson 4

View ControllersUINavigationController

• Sub-class of UIViewController• a special View Controller that manages the navigation of hierarchical content

Page 15: iOS Beginners Lesson 4

View ControllersUINavigationController

• It is a “container” that embeds content of other View Controllers inside itself

Page 16: iOS Beginners Lesson 4

Creating and Editing

Page 17: iOS Beginners Lesson 4

View ControllersAppDelegate.m

#import "AppDelegate.h"#import "Data.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ [Data getAllNotes]; return YES;}

Page 18: iOS Beginners Lesson 4

View ControllersMasterViewController.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];}

Page 19: iOS Beginners Lesson 4

View ControllersDetailViewController.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]; } }

Page 20: iOS Beginners Lesson 4

View ControllersDetailViewController.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];}

Page 21: iOS Beginners Lesson 4

View ControllersDetailViewController.m

- (void)viewWillDisappear:(BOOL)animated{ if (![self.tView.text isEqualToString:@""]) { [Data setNoteForCurrentKey:self.tView.text]; } else { [Data removeNoteForKey:[Data getCurrentKey]]; } [Data saveNotes];}

Page 22: iOS Beginners Lesson 4

Displaying & Deleting

Page 23: iOS Beginners Lesson 4

View ControllersMasterViewController.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;}

Page 24: iOS Beginners Lesson 4

View ControllersMasterViewController.m

- (void)makeObjects{ _objects = [NSMutableArray arrayWithArray:[[Data getAllNotes] allKeys]]; [_objects sortUsingComparator:^NSComparisonResult(id obj1, id obj2) { return [(NSDate *)obj2 compare:(NSDate *)obj1]; }];}

Page 25: iOS Beginners Lesson 4

View ControllersMasterViewController.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];}

Page 26: iOS Beginners Lesson 4

View ControllersMasterViewController.m

Page 27: iOS Beginners Lesson 4

View ControllersMasterViewController.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]; }}

Page 28: iOS Beginners Lesson 4

View ControllersMasterViewController.m

- (void)viewWillAppear:(BOOL)animated{ [super viewDidAppear:animated]; [self makeObjects]; [self.tableView reloadData];}

Page 29: iOS Beginners Lesson 4

View ControllersMasterViewController.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. }}

Page 30: iOS Beginners Lesson 4

OVERVIEW

• Lesson 1: Introductions

• Lesson 2: iOS specifics

• Lesson 3: Data Model

• Lesson 4: Logic (Controller) & Interface