Quick Start to iOS Development

28
Quick Start to iOS Development Jussi Pohjolainen Tampere University of Applied Sciences

description

 

Transcript of Quick Start to iOS Development

Page 1: Quick Start to iOS Development

Quick  Start  to  iOS  Development  

Jussi  Pohjolainen  Tampere  University  of  Applied  Sciences  

Page 2: Quick Start to iOS Development

Anatomy?  

Page 3: Quick Start to iOS Development

Anatomy  

•  Compiled  Code,  your's  and  framework's  

•  Nib  –  files:  UI-­‐elements  •  Resources:  images,  sounds,  strings  

•  Info.plist  –  file:  app  configuraIon    

Page 4: Quick Start to iOS Development

info.plist  <?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

<plist version="1.0">

<dict>

<key>CFBundleDevelopmentRegion</key>

<string>English</string>

<key>CFBundleDisplayName</key>

<string>${PRODUCT_NAME}</string>

<key>CFBundleExecutable</key>

<string>${EXECUTABLE_NAME}</string>

<key>CFBundleIconFile</key>

<string></string>

<key>CFBundleIdentifier</key>

<string>com.yourcompany.${PRODUCT_NAME:rfc1034identifier}</string>

<key>CFBundleInfoDictionaryVersion</key>

<string>6.0</string>

<key>CFBundleName</key>

<string>${PRODUCT_NAME}</string>

<key>CFBundlePackageType</key>

<string>APPL</string>

<key>CFBundleSignature</key>

<string>????</string> <key>CFBundleVersion</key>

<string>1.0</string>

<key>LSRequiresIPhoneOS</key>

<true/>

<key>NSMainNibFile</key>

<string>MainWindow</string>

</dict>

</plist>

Page 5: Quick Start to iOS Development

Info.plist  in  Xcode  

Page 6: Quick Start to iOS Development

nib-­‐file?  

•  Interface  Builder  is  used  for  creaIng  Uis  •  The  interface  is  stored  in  a  file  .n/xib  –  .nib  =  Next  Interface  Builder  –  .xib  =  new  version  (Interface  Builder  3)  of  nib  

•  The  xib  file  is  xml!  •  By  conven)on,  refer  to  nib  

Page 7: Quick Start to iOS Development

nib-­‐file  

Page 8: Quick Start to iOS Development

nib  –  files  in  Interface  Builder  

Page 9: Quick Start to iOS Development

main.m  // // main.m // ExampleOfiPhoneApp // // Created by Jussi Pohjolainen on 22.3.2010. // Copyright TAMK 2010. All rights reserved. // #import <UIKit/UIKit.h> int main(int argc, char *argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; int retVal = UIApplicationMain(argc, argv, nil, nil); [pool release]; return retVal; }

Page 10: Quick Start to iOS Development

UIApplicaIonMain?  

•  Creates  instance  of  UIApplicaIon  •  Scans  info.plist  •  Opens  the  UI  –  file  (xib)  that  is  defined  in  info.plist  (mainwindow.xib)  

•  Connects  to  window  server  •  Establishes  run  loop  •  Calls  method's  from  it's  delegate    

Page 11: Quick Start to iOS Development

Life  Cycle  

Page 12: Quick Start to iOS Development

Design  PaYern:  DelegaIon  

•  One  object  sends  periodically  messages  to  another  object  specified  as  its  delegate  

•  Delegate  methods  are  grouped  together  with  protocol  (Java:  Interface)  

Page 13: Quick Start to iOS Development

Delegate?  

•  Xcode  project  template  has  provided  UIApplicaIonDelegate  for  you  

•  Can  implement:  - applicationDidFinishLaunching

-  applicationWillTerminate –  applicationDidReceiveMemoryWarning –  ...

Page 14: Quick Start to iOS Development

Delegate-­‐class  in  Hello  World  

HelloWorldAppDelegate

IBOutlet UIWindow* window;

-  applicationDidFinishLaunching -  applicationWillTerminate -  applicationDidReceiveMemoryWarning -  ... -  dealloc

NSObject

UIApplicationDelegate

Page 15: Quick Start to iOS Development

Delegate  #import <UIKit/UIKit.h> @interface HelloWorldAppDelegate : NSObject

<UIApplicationDelegate> { UIWindow *window; } @property (nonatomic, retain) IBOutlet UIWindow

*window; @end

What  is  this?  

Page 16: Quick Start to iOS Development

Outlet?  

•  Outlet:  the  UI  widget  is  implemented  in  Interface  Builder!  

•  When  the  app  starts,  the  main  .nib  file  is  loaded  – MainWindow.xib  

•  This  file  contains  UIWindow  that  can  be  modified  via  the  Interface  Builder.  

 

Page 17: Quick Start to iOS Development

Delegate  classes  implementaIon  #import "HelloWorldAppDelegate.h" @implementation HelloWorldAppDelegate @synthesize window; - (void)applicationDidFinishLaunching:(UIApplication *)application { // Override point for customization after application launch [window makeKeyAndVisible]; } - (void)dealloc { [window release]; [super dealloc]; } @end

Page 18: Quick Start to iOS Development

Windows,  Views  

•  iPhone  app  has  usually  one  UIWindow  •  UIWindow  can  consist  of  UIViews.  View  can  contain  another  UIViews  (UI-­‐elements)  

•  UIWindow  – UIView  •  NSBuYon  

Page 19: Quick Start to iOS Development

Interface  Builder  

Page 20: Quick Start to iOS Development

Interface  Builder  

Page 21: Quick Start to iOS Development

In  Code  #import <UIKit/UIKit.h>

@interface HelloWorldAppDelegate : NSObject <UIApplicationDelegate> {

UIWindow *window;

UITextField *mytextfield;

}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@property (nonatomic, retain) IBOutlet UITextField *mytextfield;

@end

Page 22: Quick Start to iOS Development

Connect  the  Outlet  with  Interface  Builder  

Page 23: Quick Start to iOS Development

AcIons  

•  AcIons:  what  method  is  called  when  something  happens?  

 

Page 24: Quick Start to iOS Development

In  Code  #import <UIKit/UIKit.h>

@interface HelloWorldAppDelegate : NSObject <UIApplicationDelegate> {

UIWindow *window;

UITextField *mytextfield;

}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@property (nonatomic, retain) IBOutlet UITextField *mytextfield;

-  (IBAction) userClicked: (id) sender;

@end

Page 25: Quick Start to iOS Development

In  Interface  Builder  

Page 26: Quick Start to iOS Development

In  Code  #import "HelloWorldAppDelegate.h"

@implementation HelloWorldAppDelegate

@synthesize window;

@synthesize mytextfield;

- (IBAction) userClicked: (id) sender

{

NSString* text = [mytextfield text];

NSLog(text);

}

...

Page 27: Quick Start to iOS Development

In  Code:  Using  Alert  #import "HelloWorldAppDelegate.h"

@implementation HelloWorldAppDelegate

@synthesize window;

@synthesize mytextfield;

- (IBAction) userClicked: (id) sender

{

NSString* text = [mytextfield text];

UIAlertView *alert = [[UIAlertView alloc]

initWithTitle:@"Hello"

message:text

delegate:nil

cancelButtonTitle:@"Ok"

otherButtonTitles:nil];

[alert show];

[alert release];

}

...

Page 28: Quick Start to iOS Development

Result