Share
Embed
Application, MVC, and Views
Jayson Basañes
July 19, 2012
Transcript
14 slides
-
UIApplica'on • Every applica'on has a single instance of
UIApplica'on • Manages the status bar and applica'on icon badge • Can open a URL, schedule a local no'fica'on, register for remote no'fica'ons • Manages all open windows (UIWindow) UIApplication *app = [UIApplication sharedApplication]; -
Applica'on Delegate • An app created by XCode provides
a UIApplica'onDelegate class. • It is through the applica'on delegate that you watch for UIApplica'on events. • application:didFinishLaunchingWithOptions • applicationDidBecomeActive • applicationWillEnterForeground • applicationWillResignActive • applicationDidEnterBackground • applicationWillTerminate -
MVC Model View Controller • Presenta'on
layer • What the user sees and interacts with • App data (e.g. Core Data) • Business logic • Intermediary between model and view • Receives and acts on control events -
MVC (Example Scenario) Model View Controller
• Present login form (labels, buNons, etc.) • Tell controller if user submiNed the form • Stores user data • Provides interfaces for gePng user data • Decides what type of view to show • Waits for form submission, and uses model to validate login data -
Controls and Events View Controller IBOutlet
IBAc'on A controller’s reference to a UIView (e.g. UIBuNon) A controller method, called by UIView for its specifically aNached event (e.g. buNon click) -
Controls and Events ANaching to events through code:
- (void)viewDidLoad! {! [super viewDidLoad];! ! [self.submitButton addTarget:self action:@selector(buttonTapped:) ! forControlEvents:UIControlEventTouchUpInside];! }! ! - (IBAction)buttonTapped:(id)sender ! {! NSLog(@"Our button was tapped!");! } -
Views (UIView) • Heirarchical: has a single superview and
subviews • All live inside UIWindow. UIWindow is also a UIView. • Superviews retain their subviews -
View Structures • CGPoint: loca'on { x, y }
• CGSize: dimension { width, height } • CGRect: loca'on and dimension { origin, size } CGPoint point = CGPointMake(50, 100); CGSize size = "(150, 150); CGRect rect = CGRectMake(50, 100, 150, 150); -
UIView.frame and UIView.bounds • Frame and Bounds are of
CGRect type • Frame is rela've to the superview's coordinate system • Bounds is rela've to the local coordinate system -
UIView Coordinate System (0,0) 320 +x +y 480 (70,50)
100 90 A B View A frame View B frame View B bounds origin: 0, 0 size: 320, 480 origin: 70, 50 size: 100, 90 origin: 0, 0 size: 100, 90 -
Programma'cally crea'ng views CGRect rect = CGRectMake(10, 10, 200,
50);! UILabel *label = [[UILabel alloc] initWithFrame:rect];! label.text = @"Ouya";! ! [self.view addSubview:label];! [label release]; -
UIViewController Lifecycle -‐init -‐viewDidLoad -‐viewWillAppear -‐viewWillDisappear
-‐viewDidAppear -‐viewWillUnload -‐viewDidUnload -‐viewDidDisappear -‐dealloc -
Exercise (70,50) 100 90 A B é
ê ç è Given the illustra'on on the lef, create an applica'on that moves a view (B) depending on the pressed buNon. Rules: • The View B should be created programma'cally. No Interface Builder!