Sunday, November 3, 2013

Harvard Mobile Course Lecture on iOS6 MVC


I finally got around again to the Harvard Building Mobile Applications Course, finishing up the lecture #2 on the Apple iOS6  MVC material. The lecture (pdf) and souce code (pdf) ,The entire zipped up code can be found here.

this lecture goes over the basics of a Single View Application (MVC) implemented with a nib , along with introducing the UIAlertView

XCode - New Project -> new empty application (delegate and window), MasterDetailApplication, SingleViewApplication, Tab Application

main.m : function - it returns  UIApplicationMain() which passes in reference to AppDelegate

#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}


AppDelegate.h file:
inherits from UIResponder  - conforms to UIApplicationDelegate protocol
- has properties UIWindow *window  & ViewController  *viewController 
See: http://vimeo.com/46551853

#import <UIKit/UIKit.h>
 @class ViewController;
 @interface AppDelegateUIResponder <UIApplicationDelegate>
@property (strong, nonatomic) ViewController *viewController;
@property (strong, nonatomic) UIWindow *window;
@end

AppDelegate.m file: 
AppDelegate (.m file) - has EVENT callbacks:

  • applicationWilResignActive ()
  • ApplicationDidEnterBackground()
  • applicationWillEnterForeground()
  • applicationWillTerminate()
  • ApplicationDidBecomeActive()
  • didFinishLaunchingWithOptions()

#import "AppDelegate.h"
#import "ViewController.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}

@end

UIKit - ui sdk
UIView - something on screen (the V)
UIViewController - http://www.ralfebert.de/archive/ios/tutorial_iosdev/viewcontroller/ (the C but called ViewController)
plist is preferences files (xml).






https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html

ManagingYourApplicationsFlow
- circle is the event loop : sit waiting for you to do something or something external
- main function calls UIApplicationMain() function
- first initialization such as examples such as top scores, reference items, etc application:willFinishLaunchingWithOptions() call back
- restore ui state if you paused for example
- final ui initialization application:didFinishLaunchingWithOptions() callback
- activated app, wit for user to do something applicationDidBecomeActive ()callback
- handle events

http://www.techrepublic.com/blog/ios-app-builder/understand-the-states-and-transitions-of-an-ios-app/

program running - an apple thread can interrupt go sleep (i.e phone call)

police battery life better, may get notifications (i.e.) but have to load app

built in iOS mail more tightly coupled with the operating system





nonatomic - dont bother thread safe code, atomic - multiple threads - i.e. printing, readonly - getter, no setter




 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
. {
// refer to the window property (i.e UIWindow), allocate it, init with arguements, call mainScreen()
// bounds returns dimensions
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor] ;
// visible and key means important and return success
[self.window.makeKeyAndVisible];
return YES;


AppDelegate (.h file) - inherits from UIResponder  - conforms to UIApplicationDelegate protocol
- also has property (strong,nonatomic) ViewController * viewcontroller
- ViewController inherits from UIViewController, viewDidLoad() for any setup after when controller is loaded, didReciveMemoryWarning()


self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// @ViewController : associate Nib file with Controller
20. self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
// the most important controller
21. self.window.rootViewController = self.viewController;
22. [self.window makeKeyAndVisible];
23. return YES;

SingleViewApplication , add text field (inspector shows UITextField), add button

Create an IBOutlet property to connect GUI action to Controller UITextField

Nib: hold file owner(who owns the view) and click control (extension cord) to nib file textfield (64bit memory address), gives few outlets available, select textfield

need cable from nib back to code (opposite of outlet is IBAction)

- (IBAction)go:(id)sender

have to wire from nib to this code (ctrl from button to code i.e. file owner) go methed is presented for selection (touch up inside shown if you ctrl click button afterward)


implement the go method in .m file

- (IBAction)go:(id)sender {
 
    // wired up text field to action did 'end on exit' to owner in nib file
 
    NSLog(@"here %@", self.textField.text);
}


UIAlertView http://www.idev101.com/code/User_Interface/UIAlertView.html
http://nscookbook.com/ios-programming-quick-start-guide/




[textField resignFirstResponder]



keyboard input  - Go - alertview


Here are code take aways:


The UI Alert view

On entering text in the text field and clicking go button we get an UI Alert:

- (IBAction)go:(id)sender {
   
    // wired up text field to action did 'end on exit' to owner in nib file
   
    NSLog(@"here %@", self.textField.text);
   
    // put text field away
   
    [self.textField resignFirstResponder];
   
    NSString *textToDisplay = [NSString stringWithFormat:@"here %@", self.textField.text];
   
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hi: " message:textToDisplay
                                             delegate: self cancelButtonTitle:@"Bye" otherButtonTitles: nil];
   
    [alert show ];
   
  
}

As far as NIB file:

the button had an event of 'Touch Up Inside' with file owner of go
 the text field had an event of 'Did End On Exit' with a file owner of go , along with an Outlet of delegate.

As, the controller has a protocol as follows:
@interface ViewController : UIViewController <UIAlertViewDelegate>

Moreover, the controller had to implement this method:

- (void) alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{

    self.textField.text = nil;


}


Thus,  you enter chars in the text box, click go button, get a UIAlert presented, whereby the keyboard goes away via the call to resign first responder. At that point, the user is able to click the button on Alert and then it resets the text field.

No comments:

Post a Comment