iOS Beginners Lesson 4

496 views

Published on

iOS Beginners Lesson 4

Published in: Software
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
496
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
9
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

iOS Beginners Lesson 4

  1. 1. LOGIC & INTERFACE Building our App
  2. 2. OVERVIEW • Lesson 1: Introductions • Lesson 2: iOS specifics • Lesson 3: Data Model • Lesson 4: Logic (Controller) & Interface
  3. 3. LESSON 3: DATA MODEL • Hour 1: Storyboard • Hour 2: Creating & Editing • Hour 3: Display & Deleting Notes
  4. 4. Storyboard
  5. 5. Storyboard
  6. 6. 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
  7. 7. Storyboard • Each scene has a dock (displays icons representing the top-level objects of the scene)
  8. 8. Storyboard • The “dock” is where we make connections between code in our View Controller and its Views (“visual objects on the scene”)
  9. 9. View Controllers • A storyboard displays View Controllers and corresponding Views visually
  10. 10. View Controllers • A storyboard displays View Controllers and corresponding Views visually
  11. 11. - (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
  12. 12. View Controllers
  13. 13. View Controllers UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
  14. 14. View Controllers UINavigationController • Sub-class of UIViewController • a special View Controller that manages the navigation of hierarchical content
  15. 15. View Controllers UINavigationController • It is a “container” that embeds content of other View Controllers inside itself
  16. 16. Creating and Editing
  17. 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. 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. 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. 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. 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. 22. Displaying & Deleting
  23. 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. 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. 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. 26. View Controllers MasterViewController.m
  27. 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. 28. View Controllers MasterViewController.m - (void)viewWillAppear:(BOOL)animated { [super viewDidAppear:animated]; [self makeObjects]; [self.tableView reloadData]; }
  29. 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. 30. OVERVIEW • Lesson 1: Introductions • Lesson 2: iOS specifics • Lesson 3: Data Model • Lesson 4: Logic (Controller) & Interface

×