Download - iOS Beginners Lesson 4
![Page 1: iOS Beginners Lesson 4](https://reader036.vdocument.in/reader036/viewer/2022062307/5565a98ad8b42a083a8b4e38/html5/thumbnails/1.jpg)
LOGIC & INTERFACEBuilding our App
![Page 2: iOS Beginners Lesson 4](https://reader036.vdocument.in/reader036/viewer/2022062307/5565a98ad8b42a083a8b4e38/html5/thumbnails/2.jpg)
OVERVIEW
• Lesson 1: Introductions
• Lesson 2: iOS specifics
• Lesson 3: Data Model
• Lesson 4: Logic (Controller) & Interface
![Page 3: iOS Beginners Lesson 4](https://reader036.vdocument.in/reader036/viewer/2022062307/5565a98ad8b42a083a8b4e38/html5/thumbnails/3.jpg)
LESSON 3: DATA MODEL
• Hour 1: Storyboard
• Hour 2: Creating & Editing
• Hour 3: Display & Deleting Notes
![Page 4: iOS Beginners Lesson 4](https://reader036.vdocument.in/reader036/viewer/2022062307/5565a98ad8b42a083a8b4e38/html5/thumbnails/4.jpg)
Storyboard
![Page 5: iOS Beginners Lesson 4](https://reader036.vdocument.in/reader036/viewer/2022062307/5565a98ad8b42a083a8b4e38/html5/thumbnails/5.jpg)
Storyboard
![Page 6: iOS Beginners Lesson 4](https://reader036.vdocument.in/reader036/viewer/2022062307/5565a98ad8b42a083a8b4e38/html5/thumbnails/6.jpg)
• 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](https://reader036.vdocument.in/reader036/viewer/2022062307/5565a98ad8b42a083a8b4e38/html5/thumbnails/7.jpg)
• Each scene has a dock (displays icons representing the top-level objects of the scene)
Storyboard
![Page 8: iOS Beginners Lesson 4](https://reader036.vdocument.in/reader036/viewer/2022062307/5565a98ad8b42a083a8b4e38/html5/thumbnails/8.jpg)
• 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](https://reader036.vdocument.in/reader036/viewer/2022062307/5565a98ad8b42a083a8b4e38/html5/thumbnails/9.jpg)
• A storyboard displays View Controllers and corresponding Views visually
View Controllers
![Page 10: iOS Beginners Lesson 4](https://reader036.vdocument.in/reader036/viewer/2022062307/5565a98ad8b42a083a8b4e38/html5/thumbnails/10.jpg)
• A storyboard displays View Controllers and corresponding Views visually
View Controllers
![Page 11: iOS Beginners Lesson 4](https://reader036.vdocument.in/reader036/viewer/2022062307/5565a98ad8b42a083a8b4e38/html5/thumbnails/11.jpg)
- (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](https://reader036.vdocument.in/reader036/viewer/2022062307/5565a98ad8b42a083a8b4e38/html5/thumbnails/12.jpg)
View Controllers
![Page 13: iOS Beginners Lesson 4](https://reader036.vdocument.in/reader036/viewer/2022062307/5565a98ad8b42a083a8b4e38/html5/thumbnails/13.jpg)
View ControllersUISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
![Page 14: iOS Beginners Lesson 4](https://reader036.vdocument.in/reader036/viewer/2022062307/5565a98ad8b42a083a8b4e38/html5/thumbnails/14.jpg)
View ControllersUINavigationController
• Sub-class of UIViewController• a special View Controller that manages the navigation of hierarchical content
![Page 15: iOS Beginners Lesson 4](https://reader036.vdocument.in/reader036/viewer/2022062307/5565a98ad8b42a083a8b4e38/html5/thumbnails/15.jpg)
View ControllersUINavigationController
• It is a “container” that embeds content of other View Controllers inside itself
![Page 16: iOS Beginners Lesson 4](https://reader036.vdocument.in/reader036/viewer/2022062307/5565a98ad8b42a083a8b4e38/html5/thumbnails/16.jpg)
Creating and Editing
![Page 17: iOS Beginners Lesson 4](https://reader036.vdocument.in/reader036/viewer/2022062307/5565a98ad8b42a083a8b4e38/html5/thumbnails/17.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062307/5565a98ad8b42a083a8b4e38/html5/thumbnails/18.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062307/5565a98ad8b42a083a8b4e38/html5/thumbnails/19.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062307/5565a98ad8b42a083a8b4e38/html5/thumbnails/20.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062307/5565a98ad8b42a083a8b4e38/html5/thumbnails/21.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062307/5565a98ad8b42a083a8b4e38/html5/thumbnails/22.jpg)
Displaying & Deleting
![Page 23: iOS Beginners Lesson 4](https://reader036.vdocument.in/reader036/viewer/2022062307/5565a98ad8b42a083a8b4e38/html5/thumbnails/23.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062307/5565a98ad8b42a083a8b4e38/html5/thumbnails/24.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062307/5565a98ad8b42a083a8b4e38/html5/thumbnails/25.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062307/5565a98ad8b42a083a8b4e38/html5/thumbnails/26.jpg)
View ControllersMasterViewController.m
![Page 27: iOS Beginners Lesson 4](https://reader036.vdocument.in/reader036/viewer/2022062307/5565a98ad8b42a083a8b4e38/html5/thumbnails/27.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062307/5565a98ad8b42a083a8b4e38/html5/thumbnails/28.jpg)
View ControllersMasterViewController.m
- (void)viewWillAppear:(BOOL)animated{ [super viewDidAppear:animated]; [self makeObjects]; [self.tableView reloadData];}
![Page 29: iOS Beginners Lesson 4](https://reader036.vdocument.in/reader036/viewer/2022062307/5565a98ad8b42a083a8b4e38/html5/thumbnails/29.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062307/5565a98ad8b42a083a8b4e38/html5/thumbnails/30.jpg)
OVERVIEW
• Lesson 1: Introductions
• Lesson 2: iOS specifics
• Lesson 3: Data Model
• Lesson 4: Logic (Controller) & Interface