ios beginners lesson 4
Post on 27-May-2015
209 Views
Preview:
DESCRIPTION
TRANSCRIPT
LOGIC & INTERFACEBuilding 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
• 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
• 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”)
Storyboard
• A storyboard displays View Controllers and corresponding Views visually
View Controllers
• A storyboard displays View Controllers and corresponding Views visually
View Controllers
- (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 ControllersUISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
View ControllersUINavigationController
• Sub-class of UIViewController• a special View Controller that manages the navigation of hierarchical content
View ControllersUINavigationController
• It is a “container” that embeds content of other View Controllers inside itself
Creating and Editing
View ControllersAppDelegate.m
#import "AppDelegate.h"#import "Data.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ [Data getAllNotes]; return YES;}
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];}
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]; } }
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];}
View ControllersDetailViewController.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 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;}
View ControllersMasterViewController.m
- (void)makeObjects{ _objects = [NSMutableArray arrayWithArray:[[Data getAllNotes] allKeys]]; [_objects sortUsingComparator:^NSComparisonResult(id obj1, id obj2) { return [(NSDate *)obj2 compare:(NSDate *)obj1]; }];}
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];}
View ControllersMasterViewController.m
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]; }}
View ControllersMasterViewController.m
- (void)viewWillAppear:(BOOL)animated{ [super viewDidAppear:animated]; [self makeObjects]; [self.tableView reloadData];}
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. }}
OVERVIEW
• Lesson 1: Introductions
• Lesson 2: iOS specifics
• Lesson 3: Data Model
• Lesson 4: Logic (Controller) & Interface
top related