Thursday, November 7, 2013

Harvard Building Mobile Applications Course Week 3 iOS6

On to the week 3 lecture transcript  and video of the Harvard Building Mobile Applications Course . The lecture slides , code slides as pdf , and code supplement the presentation.



UI Kit : API for UI related things

Templates:

  • Empty Application
  • Single View Application
  • Utility Application
  • Master-Detail Application 
  • Tabbed Application
Program #1:
program flow : main.m -> AppDelegate:
@class ViewController - forward declaration to avoid circular references

didFinishLaunchingWithOptions method  :
self viewController  given ViewController class
self window rootViewController given the self viewController

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
19. {
20. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
21. self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
22. self.window.rootViewController = self.viewController;
23. [self.window makeKeyAndVisible];
24. return YES;
25. }

Program #2:
Master-Detail application  example -see View Controller types:

The App Delegate header file which provides a navigation controller:
@interface AppDelegate : UIResponder <UIApplicationDelegate>
16. @property (strong, nonatomic) UINavigationController *navigationController;


example created a instance of class MasterViewController in AppDelegate.
- The root controller here is navigation controller  with  master controller:
- self navigationController which is a UINavigationController which is initialized with MasterViewController instance


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
18. {
19. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
20. MasterViewController *masterViewController = [[MasterViewController alloc] initWithNibName:@"MasterViewController" bundle:nil];
21. self.navigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
22. self.window.rootViewController = self.navigationController;
23. [self.window makeKeyAndVisible];
24. return YES;

MasterViewController -> UITableViewController -> UIViewController

Te header file shows MasterViewController provides a detailViewController:

@interface MasterViewController : UITableViewController
17. 
18. @property (strong, nonatomic) DetailViewController *detailViewController;
19. 
20. @end


The implementation of MasterViewController has a init method for nib:
- the method is passed from the AppDelegate: @"MasterViewController" bundle:nil];
- The top of the navigation list shows a title of Master


22. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
23. {
24. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
25. if (self) {
26. self.title = NSLocalizedString(@"Master", @"Master");
27. }
28. return self;
29. }
30.


MasterViewController:
- method viewDidLoad invoked when the particular view loads such as adding things to view
- method numberOfSectionsInTableView is how many sections in table
- method numberOfRows inSection is how many entries in section
NSIndexPath is row and section
UIBarButtonSystemItemAdd  constant of enum  of UIBarButtonItem
UITableViewCell is the cell in view
- When view loads adds edit button after calling parent and drags item into navigation bar

- (void)viewDidLoad
32. {
33. [super viewDidLoad];
34. self.navigationItem.leftBarButtonItem = self.editButtonItem;
35.
36. UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(
insertNewObject:)];
37. self.navigationItem.rightBarButtonItem = addButton;
38. }

sections are grouping of rows.
 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
53. {
54. return 1;
55. }
56.

how many rows

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
58. {
59. return _objects.count;
60. }
61.

add new row (method from the selector when creating nib). Index path is section and row.

- (void)insertNewObject:(id)sender
41. {
42. if (!_objects) {
43. _objects = [[NSMutableArray alloc] init];
44. }
45. [_objects insertObject:[NSDate date] atIndex:0];
46. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
47. [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];lectures/3/src3/MasterDetail/MasterDetail/MasterViewController.m
48. }


Cell population:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
63. {
64. static NSString *CellIdentifier = @"Cell";
65.
66. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
67. if (cell == nil) {
68. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
69. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
70. }
71.
72. NSDate *object = _objects[indexPath.row];
73. cell.textLabel.text = [object description];
74. return cell;
75. }

Program #3 (storyboards)
storyboard - simply user interface process
started with utility application
Looks like 2 nibs same time with transition in middle (known as segue)
specify in code what you want to make happen
Right side of scene you can see transition details : identifier: ShoAlternate, type is Modal, FlipHorizontal
There is an action on button
inform Destination Controller that delegate will be self


Program #4 (Gestures)

Added the images to the project
Have the UIImageView in the nib



@interface ViewController ()
// private properties

@property (nonatomic, readwrite, weak) IBOutlet UIImageView *imageView;
@property (assign, nonatomic, readwrite) int index;
@property (nonatomic, readwrite, strong) NSArray *robs;

// private methods
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;
- (void)handleLongPress:(UILongPressGestureRecognizer *)sender;
- (void)handleSwipe:(UISwipeGestureRecognizer *)sender;


@end


INITIALIZE array:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
  ...
    
        // prepare Robs
        self.robs = [[NSArray alloc] initWithObjects:@"rob1.jpg", @"rob2.jpg", @"rob3.jpg", nil];
        self.index = 0;
    }
    return self;

}





- (void)viewDidLoad:

// load image
    self.imageView.image = [UIImage imageNamed:[self.robs objectAtIndex:self.index]];




- (void)handleSwipe:(UISwipeGestureRecognizer *)sender
{
    // handle swipe
    UISwipeGestureRecognizerDirection direction = [(UISwipeGestureRecognizer *)sender direction];
    switch (direction)
    {
        // ignore up, down
        case UISwipeGestureRecognizerDirectionUp:
        case UISwipeGestureRecognizerDirectionDown:
            break;
            
        // left
        case UISwipeGestureRecognizerDirectionLeft:
            self.index = (self.index + 1) % [self.robs count];
            break;
            
        // right
        case UISwipeGestureRecognizerDirectionRight:
            self.index = (self.index + [self.robs count] - 1) % [self.robs count];
            break;           
    }
    
    // update Rob
    self.imageView.image = [UIImage imageNamed:[self.robs objectAtIndex:self.index]];

}


Gestures -UI*GetsureRecognizer .i.e. UIPinchGestureRecognizer



storage

NSDefaults - interface for storing persisently, settings, configurable items
SqlLite store using api, written in c
xml, json
Core Data
Property Lists - dictionary of strings , plist files - xml, key value pairs:

PList program




No comments:

Post a Comment