iPhone Programming - PowerPoint PPT Presentation

About This Presentation
Title:

iPhone Programming

Description:

iPhone Programming Version 2 Created by Nathan Magnus – PowerPoint PPT presentation

Number of Views:120
Avg rating:3.0/5.0
Slides: 104
Provided by: Office20041902
Category:

less

Transcript and Presenter's Notes

Title: iPhone Programming


1
iPhone Programming
  • Version 2
  • Created by Nathan Magnus

2
Apple
  • Your most useful resource is the Apple developer
    documentation.
  • http//developer.apple.com/

3
Xcode IDE
  • Apple provides an IDE for creating iPhone
    applications
  • Xcode can also be used to create OSX applications

4
Xcode IDE
  • Installation
  • Download from http//developer.apple.com/iphone/in
    dex.action
  • You will need to create an account if you do not
    already have one
  • Mount by clicking on it in the download directory
  • Double click on iPhone SDK

5
iPhone Simulator
  • Simulates an iPhone.
  • Can be rotated by 90 to the right or left
    through the Hardware menu
  • Supports multiple touches (option and
    option-shift)
  • Known bugs
  • Cannot be rotated upside down
  • When rotated there is a small area that will not
    respond to touches

6
An IPhone Application
  • Create an application that loads Google in a
    UIWebView
  • The user enters a URL into the text field and
    when they click enter on the keyboard the
    UIWebView loads that address

7
Creating the Program
  • Create a new View-Based application in Xcode
  • File -gt New Project...
  • Select View-Based Application and click
    Choose...
  • Name your project webNav and save

8
Program 1
  • In the upper pane, double click on
    webNavViewController.xib
  • This will open the interface builder (IB)

9
Program 1
  • Ensure the Library window is open (Tools-gt
    Library)

10
Program 1
  • Under Data View drag and drop a Web View onto
    the main view

11
Program 1
12
Program 1
  • Click on webNavViewController.h in Xcode

13
Program 1
  • Add the following lines of code
  • Under _at_interface webNavViewController
    UIViewController
  • Add IBOutlet UIWebView webView
  • Under before _at_end
  • Add _at_property (nonatomic, retain) UIWebView
    webView

14
Program 1
  • Return to the Interface Builder
  • Click on the WebView in the main View

15
Program 1
  • Ensure that the Connections Inspector is open
    (Tools -gt Connections Inspector)

16
Program 1
  • Click on the circle beside New Referencing
    Outlet and drag to Files Owner

17
Program 1
  • Select webView from the two options

18
Program 1
  • Save the Interface Builder
  • Double Click on webNavViewcontroller.m

19
Program 1
  • Uncomment -(void)viewDidLoad function

20
Program 1
  • Under super viewDidLoad
  • Create a NSURL
  • NSURL url NSURL URLWithString_at_http//www.goo
    gle.ca
  • The _at_ makes the following string a NSString
  • Create a NSURLRequest
  • NSURLRequest request NSURLRequest
    requestwithURLurl
  • Load the request
  • webView loadRequestrequest

21
Program 1
22
Program 1
  • This is what the program should look like

23
Information
  • Files (.m files can be renamed to .mm to allow
    Objective-C)
  • ProjectNameAppDelegate (.h and .m) - Creates the
    window and view
  • ProjectNameViewController (.h and .m) - Controls
    user interaction with the view
  • viewDidLoad is called when the initial view loads

24
Program 2
  • Create an application that has a UITextField,
    UILabel, UIButton and UIImageView
  • Screen begins with an image of a your
    handsome/beautiful instructor (or a hated
    celebrity), a UITextField and a UIButton (with an
    appropriate title)
  • The user enters a phrase into a UITextField.
    After they are done editing they can click on a
    UIButton.
  • If the phrase they entered matches a certain
    phrase then the image changes to an explosion and
    a label appears telling the user they just blew
    up the instructor.
  • If the phrase does not match, a message telling
    them to try again should be displayed.

25
Program 2
  • Steps
  • Find an image of your handsome/beautiful
    instructor or a hated celebrity and save it to
    Documents
  • Find an image of an explosion and save it to
    Documents
  • Create a new View-Based Application (call it
    Boom
  • Create the Interface using IB
  • Add a UILabel (found in Inputs Values)
  • Add a UIImageView (found in Data Views)
  • Add a UITextField (found in Inputs Values)
  • Top half of screen due to onscreen keyboard
  • Add a UIButton (found in Inputs Values)

26
Program 2
27
Program 2
  • Click on the Attributes Inspector tab (or
    Tools-gtAttributes Inspector)

28
Program 2
  • Select the button and add the title Check Input

29
Program 2
  • Navigate to your saved image using Finder
  • Drag and drop your images into the Resources
    group of the Xcode project

30
Program 2
  • Select Copy items into destination groups
    folder (if needed) and ensure other settings are
    all defaults then click Add

31
Program 2
32
Program 2
  • Click on the UIImageView in the IB and add your
    image in the Attributes Inspector

33
Program 2
  • Open the Connection Inspector
  • (Tools-gtConnections Inspector)
  • Select each object on the interface builder
    (UITextField, UIButton, UIImageView, UILabel),
    drag to Files Owner and select the appropriate
    option
  • For the UITextField, click on the circle beside
    delegate and drag to Files Owner

34
Program 2
35
Program 2
  • Add to BoomViewController.h
  • After _at_interface BoomViewController
    UIViewController
  • IBOutlet UILabel label
  • IBOutlet UITextField input
  • IBOutlet UIImageView image
  • IBOutlet UIButton button

36
Program 2
  • After before _at_end
  • _at_property (nonatomic, retain) UILabel label
  • _at_property (nonatomic, retain) UITextField input
  • _at_property (nonatomic, retain) UIImageView image
  • _at_property (nonatomic, retain) UIButton button
  • -(BOOL)textFieldShouldReturn (UITextField)theTex
    tField
  • -(void)checkInput

37
Program 2
38
Program 2
  • In BoomViewController.m
  • Uncomment the (void)viewDidLoad method
  • Add after super viewDidLoad
  • label setText_at_Enter the detonation code.
  • button addTargetself action_at_selector(checkInput
    ) forcontrolEventsUIControlEventTouchUpInside

39
Program 2
40
Program 2
  • Add after the (void)viewDidLoad function
  • -(BOOL)textFieldShouldReturn (UITextField)theTex
    tField
  • theTextField resignFirstResponder
  • return YES

41
Program 2
42
Program 2
  • Add after the -(void)viewDidLoad method
  • -(void)checkInput
  • NSString userInput input text
  • if(userInput compare_at_Boom! NSOrderedSame)
  • label setText_at_Your instructor went BOOM!
  • image setImageUIImage imageNamed_at_explosion.
    jpg
  • else
  • label setTextNSString stringWithCStringThat
    is not the correct detonation code.
  • image setImageUIImage imageNamed_at_nathan.jpg

43
Program 2
44
Program 2
  • Finished Product

45
Program 2
46
NSArray
  • Used to hold objects in an array
  • id is the default object type. All objects
    (NSUInteger, NSString, etc) can be passed as an
    id
  • Initializing
  • (id)arrayWithObjects(id)obj1, (id)obj2,,nil -
    nil terminated list of objects to be added to the
    array
  • Accessing
  • -(id)objectAtIndex(NSUInteger)index - return the
    id of the object at the given index
  • Functions also exist for sorting, comparing and
    obtaining information about the array

47
Objective-C
  • Variables
  • Types include char, int, unsigned, float, double,
    BOOL
  • Constants declared using const keyword
  • declared by type name
  • Ex) float floatVal char character
  • Conditional structures
  • if(condition) code
  • else if(condition) code
  • else (condition) code
  • switch (condition)
  • case constant code
  • break
  • default
  • break

Comparison Operators
Equal
lt Less or Equal
gt Greater or Equal
lt Less
gt Greater
! Not Equal
48
Objective-C
  • Loops
  • do code while(condition)
  • while(condition) code
  • for(initialize condition increment) code
  • Arrays
  • Declaration
  • type namesizeOfArray
  • Initialize
  • type name value1, value2
  • name0value1 name1value2
  • Pointers
  • Objects when using Object method must be
    pointers or class name
  • Initialize
  • type name
  • nil/null - indicates no object at pointer but if
    used will not cause crash

49
Example of Objective-C
  • -(void)method
  • const unsigned loopAmt 10
  • printf("Testing ")
  • for(unsigned i0 iltloopAmt i)
  • printf("i...", i)
  • //prints Testing0123456789

50
Exercise
  • 1) Create a web navigation application with
    UITextField and UIWebView
  • The user enters a URL into the text field and
    when they click enter on the keyboard the
    UIWebView loads that address
  • 2) Create a simple game of Tic Tac Toe
  • Different status messages should be displayed on
    the screen (most likely using a UILabel)
  • When a game ends, nothing can be pushed except a
    button that asks if the user would like to play
    again

51
Objective-C Classes
  • Interface declared by _at_interface
    ClassNameSuperClass and ended by _at_end
  • SuperClass must always be Object or a subclass
    of Object (NSObject for the iPhone)
  • Implementation declared by _at_implementation
    ClassName
  • All class methods are public
  • Instance variables are private by default
  • Classes can be extended with categories (no
    example will be given)

52
Example of Classes
  • //tempClass inherits from NSObject
  • _at_interface tempClass NSObject
  • _at_private
  • int var //private instance variable
  • //function prototypes
  • (int)staticMethod
  • -(int)method
  • _at_end //end of interface

53
Example of Classes
  • _at_implementation tempClass
  • (int)staticMethod
  • var 200
  • return var
  • -(int)method
  • var 100 //assign a value to var
  • return var //return value of var
  • _at_end

54
Objects
  • Since every object must extend the NSObject class
    (or a subclass of it) to create a new object call
    the alloc method
  • Ex) tempClass tc tempClass alloc
  • If a method is static, it can be called without
    allocating the object
  • Ex) tempClass staticMethod

55
Objective-C
  • Syntax almost identical to Objective-C
  • Allows the use of C libraries and functions
    (ie ltiostreamgt, ltfstreamgt)
  • .h extension for header files and .mm extension
    for implementation files

56
Example of Objective-C
  • include ltiostreamgt
  • -(int)getInteger
  • return 5
  • int aFunction(id this2)
  • /print the value returned by the getInteger
    function of the object passed to the method/
  • cout ltlt this2 getInteger
  • return 10

57
Info.plst
  • Info.plst file contains information about your
    project
  • Application icon (must be included as a resource)
  • Main nib file

58
NSString
  • Full list of functions and properties can be
    found at http//developer.apple.com/documentati
    on/Cocoa/Reference/Foundation/Classes/NSString_Cla
    ss/Reference/NSString.html
  • Creating
  • Add _at_ in front of a regular string
  • ex) _at_Hello World!
  • NSString stringWithCStringHello World!
  • Many other methods including from file and URL
  • Methods
  • length, lowercaseString, uppercaseString,
    capitalizedString, compare(NSString)aString
  • NSOrderedAscending, NSOrderedSame,
    NSOrderedDescending

59
Common Information
  • hidden - whether the object is visible or not
  • Ex) label.hidden NO
  • userInteractionEnabled - whether the object will
    respond to a touch or not
  • Ex) label.userInteractionEnabled YES
  • center - CGPoint representing the center of the
    object (CGPoint is a struct with x and y fields)
  • Ex) cout ltlt label.center.x

60
UIButton
  • Found in IB library under Inputs Values
  • Setters
  • setBackgroundImage(UIImage) forState(UIControlS
    tate)
  • setImage(UIImage) forState(UIControlState)
  • setTitle(NSString) forState(UIControlState)
  • Properties
  • currentTitle, currentTitleColor, currentImage,
    currentBackgroundImage, hidden
  • Ex) aButton.hidden YES

61
UIImageView
  • Found in IB library under Data Views
  • Initializers
  • initWithImage(UIImage)
  • UIImage imageNamed(NSString)name is useful
  • Properties
  • image, userInteractionEnabled, hidden

62
UITextView
  • Found in IB library under Data Views
  • UITextAlignment - UITextAlignmentRight,
    UITextAlignmentLeft, UITextAlignmentCenter.
  • UIColor allows allows for creation of custom
    colors as well as presets such as greyColor,
    redColor, greenColor, etc
  • Properties
  • text, font, textColor, editable, textAlignment,
    hidden

63
UITextField
  • Found in IB library under Inputs Values
  • Properties
  • text, font, textColor, textAlignment, hidden

64
Closing the Keyboard
  • Keyboard automatically displays on the lower half
    of the screen when a field that allows input is
    touched
  • Keyboard does not automatically close
  • Must set the delegate (via the IB connection
    manager window) for the textView, textField or
    other object that brings up the keyboard to
    Files Owner
  • //when return is touched on the keyboard, this is
    called (BOOL)textFieldShouldReturn
    (UITextField)theTextField
  • theTextField resignFirstResponder
  • return YES

65
UILabel
  • Found in IB library under Inputs Values
  • Properties
  • text, font, textColor, textAlignment, enabled,
    adjustsFontSizeToFitWidth, minimumFontSize,
    numberOfLines, highlightedTextColor, hidden

66
NSURL/NSURLRequest
  • NSURL Functions
  • URLWithString(NSString)string
  • initWithString(NSString)string
  • URLWithString(NSString)string
    relativeToURL(NSURL)url
  • initWithString(NSString)string
    relativeToURL(NSURL)url
  • NSURLRequest Functions
  • initWithURL(NSURL)url

67
UIWebView
  • Found in IB library under Data Views
  • Properties
  • canGoBack, canGoForward, loading
  • Methods
  • goBack, goForward, stopLoading, reload,
    loadRequest(NSURLRequest)request

68
Creation Without IB
  • All UI Objects can be created without using the
    IB
  • Create a CGRect variable to hold the object using
    CGRectMake(x-offset, y-offset, width, height)
  • Ex) CGRect r CGRectMake(20.0, 20.0, 100.0,
    40.0)
  • Allocate the memory for and initialize the object
    using initWithFrame
  • Ex) UILabel label UILabel alloc
    initWithFramer
  • Set object specific properties
  • Ex) label setText_at_Label Created
  • Add to the appropriate view
  • Ex) self addSubviewlabel

69
Selectors
  • Selectors are written into a table and used at
    runtime to reference a method
  • Class methods and instance methods with the same
    name would be assigned the same selector
  • The _at_selector() directive allows the programmer
    to refer to a compiled selector rather than
    method name
  • To define a selector use SEL
  • Ex) SEL aSelector _at_selector(toSelect)
  • Other methods can be used to get selector
    information
  • NSSelectorFromString(NSString)
  • NSStringFromSelector(SEL)

70
Selectors
  • Methods that use Selectors
  • performSelector(SEL)
  • performSelector(SEL) withObject(id)
  • performSelector(SEL) withObject(id)
    withObject(id)

71
Selector Example
  • -(void)method2(int)i int2(int)j
  • //outputs 12 to the debug console
  • cout ltlt i intValue ltlt j intValue
  • -(void)method
  • NSNumber i NSNumber numberWithInt1
  • NSNumber j NSNumber numberWithInt2
  • SEL s _at_selector(method2int2)
  • self performSelectors withObjecti
    withObjectj
  • //outputs method2int2 to debug console
  • cout ltlt NSStringFromSelector(s) cString

72
Add a Target Method
  • Can add an action to a UI object when it is
    touched
  • backButton addTargetwebView action_at_selector(goB
    ack) forControlEventsUIControlEventTouchUpInside

73
Multiple Views with IB
  • Multiple views in Interface Builder can be
    displayed and hidden by their hidden property
  • Multiple windows can be displayed and hidden by
    their hidden property

74
Touches
  • Four functions can be overwritten (in EAGLView or
    ProjectNameViewController)
  • -(void)touchesBegan(NSSet)touches
    withEvent(UIEvent)event - Called when finger(s)
    touch the display
  • -(void)touchesMoved(NSSet)touches
    withEvent(UIEvent)event - Called when finger(s)
    are moved on the display
  • -(void)touchesEnded(NSSet)touches
    withEvent(UIEvent)event - Called when
    fingers(s) are removed from the display
  • -(void)touchesCancelled(NSSet)touches
    withEvent(UIEvent)event - Called if the system
    interrupts your application

75
Using Touches
  • In order for information about multiple touches
    to be passed, must set self.multipleTouchEnabled
    YES
  • The functions parameters can be used to get
    information about the touches
  • touches allObjects objectAtIndex0 - will
    return the id of the first touch on the display.
  • This id can be used to call the
    locationInViewself function which returns a
    CGPoint struct (holds x and y coordinates)

76
Example of Multiple Touches
  • -(void) touchesEnded(NSSet)touches
    withEvent(UIEvent)event
  • CGPoint touch
  • int touchCount event allTouches count
  • self.multipleTouchEnabled YES
  • for(int i0 ilttouchCount i)
  • touch touches allObjectsobjectAtIndexi
    locationInViewself
  • printf("Touch i at (f,f)\n", i1, touch.x,
    touch.y)

77
Orientation
  • iPhone has built in accelerometer that can be
    used to determine orientation
  • Need to turn orientation notifications on
  • UIDevice currentDevice beginGeneratingDeviceOri
    entationNotifications
  • Can access by accessing UIDevice
    currentDevice.orientation
  • 0 represents state unknown (UIDeviceOrientationUnk
    nown)
  • 1 represent upright state in simulator
    (UIDeviceOrientationPortrait)
  • 3 represents landscape view with left side down
    (UIDeviceOrientationLandscapeLeft)
  • 4 represents landscape view with right side down
    (UIDeviceOrientationLandscapeRight)

78
Exercise
  • Create a program that draws 2 shapes. These
    shapes will move toward the new bottom of the
    screen if it is rotated.

79
Exercise
  • Create a program that draws a textured shape in
    the middle of the screen
  • If two fingers are moved in parallel the image
    moves in the direction of the fingers movement
  • If two fingers move apart or towards each other
    the shape is enlarged or shrunk

80
Exercise
  • Create a program that draws a shape. Every 0.2
    seconds the object moves to right, left, up or
    down.
  • Use touch events to control the direction that
    the object moves

81
OpenGL ES Application
  • Draw, rotate and move shapes
  • Change colors and sizes at run-time
  • Files (.m files can be renamed to .mm to allow
    Objective-C)
  • EAGLView (.h and .m) - Already imports OpenGL ES
    library
  • -(void)drawView - is called every
    animationInterval seconds
  • Other Functions already given include
    startAnimation, stopAnimation, setAnimationInterva
    l, setAnimationTimer, dealloc, createFramebuffer,
    destroyFramebuffer and initWithCoder
  • ProjectNameAppDelegate (.h and .m) - Creates the
    window and view

82
NSTimer
  • Useful class that is frequently used to call a
    method periodically based on time
  • Methods
  • NSTimer scheduledTimerWithTimeInterval(float
    seconds) target(id target) selector_at_selector(met
    hod name) userInfonil repeats(YES or NO)

83
OpenGL ES
  • Some important OpenGL ES functions
  • glEnable(capability) and glDisable(capability) -
    enable or disable the capabilities declared by a
    constant
  • GL_TEXTURE_2D - allow textures to be mapped to 2D
    objects
  • GL_LIGHTING - allow lighting to modify colors
  • GL_FOG - blend a fog color into the post
    texturing color
  • GL_BLEND - allows for transparency

84
OpenGL ES
  • glClientEnableState(capability) and
    glClientDisableState(capability) - enable and
    disable a capability
  • GL_VERTEX_POINTER - vertices of an object are
    defined by an array of points
  • GL_COLOR_POINTER - colors at vertices are defined
    by an array of points
  • GL_TEXTURE_COORD_POINTER - map texture
    coordinates in an array to the texture

85
OpenGL ES
  • glTranslatef(x, y, z) - move the object on the
    coordinate plane by x, y and z
  • glRotatef(x, y, z) - rotate the object around the
    line defined by x, y and z
  • glDrawArrays(mode, first, count)
  • mode - the way to draw the object
  • GL_TRIANGLE_FAN, GL_TRIANGLE_STRIP, GL_LINE_LOOP,
    GL_LINES
  • first - the element in the array that represents
    the first point
  • count - the number of points in the array

86
OpenGL ES
  • glBlendFunc(sfactor, dfactor)
  • sfactor - specifies how RGBA source blending
    computed (use GL_SRC_ALPHA)
  • dfactor - specifies how RGBA destination blending
    computed (use GL_ONE_MINUS_SRC_ALPHA)
  • glTexEnvf(target, pname, param) /
    glTexEnvi(target,pname,param)
  • target - specifies the texture environment
    (GL_TEXTURE_ENV)
  • pname - single valued texture environment
    parameter (GL_TEXTURE_ENV_MODE)
  • param - symbolic constant (GL_DECAL)

87
OpenGL ES
  • glVertexPointer(dimensions, type, stride,
    vertexPointer)
  • glColorPointer(values, type, stride,
    colorPointer)
  • glColor4f(red, green, blue, alpha) /
    glColor4ub(red, green, blue, alpha)
  • glTexCoordPointer(dimensions, type,
    texturePointer)
  • glBindTexture(type, texture)

88
OpenGL ES
  • glMatrixMode(type) - the type of view being
    worked with
  • GL_MODELVIEW - Position of the model relative to
    the screen
  • GL_PROJECTION - View seen on the screen
  • glPushMatrix() - save the current view to a stack
  • glPopMatrix() - retrieve the last view that was
    pushed onto the stack
  • glLoadIdentity() - replaces the current matrix
    with the identity matrix (resets to defaults)

89
OpenGL ES
  • glOrthof(xMin, xMax, yMin, yMax, zMin, zMax) -
    set up the coordinate plan
  • xMin - the lowest x value visible
  • xMax - the highest x value visible
  • yMin - the lowest y value visible
  • yMax - the highest y value visible
  • zMin - the lowest z value visible
  • zMax - the highest z value visible

90
Load Textures
  • -(void)loadTexture(NSString )name
    intoLocation(GLuint)location
  • CGImageRef textureImage UIImage
    imageNamedname.CGImage
  • if (textureImage nil) //If the image
    doesnt exist, print an error
  • NSLog(_at_"Failed to load texture image")
  • return
  • NSInteger texWidth CGImageGetWidth(textureImage
    ) //get the width
  • NSInteger texHeight CGImageGetHeight(textureIma
    ge) //get the height
  • GLubyte textureData (GLubyte
    )malloc(texWidth texHeight 4)
  • CGContextRef textureContext
    CGBitmapContextCreate(textureData,texWidth,
    texHeight,8, texWidth 4, CGImageGetColorSpace
    (textureImage), kCGImageAlphaPremultipliedLast)
  • CGContextTranslateCTM(textureContext, 0,
    texHeight)
  • CGContextScaleCTM(textureContext, 1.0, -1.0)
  • CGContextDrawImage(textureContext,
    CGRectMake(0.0, 0.0, (float)texWidth,
    (float)texHeight), textureImage)
  • CGContextRelease(textureContext)
  • glBindTexture(GL_TEXTURE_2D, location)
  • glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
    texWidth, texHeight, 0, GL_RGBA,
    GL_UNSIGNED_BYTE, textureData)
  • free(textureData)
  • glTexParameterf(GL_TEXTURE_2D,
    GL_TEXTURE_MIN_FILTER, GL_LINEAR)
  • glTexParameterf(GL_TEXTURE_2D,
    GL_TEXTURE_MAG_FILTER, GL_LINEAR)

91
glScale
  • glScalef(GLfloat x, GLfloat y, GLfloat x)
  • glScaled(GLdouble x, GLdouble y, GLdouble z)
  • Scale the current view matrix by x, y and z

92
Example
  • float vertices
  • 1, 1,
  • 1, 0,
  • 0, 0,
  • 0, 1
  • glEnable(GL_BLEND)
  • glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
  • glMatrixMode(GL_MODELVIEW)
  • glPushMatrix()
  • glTranslatef(5, 5, 0)
  • glScalef(0.5, 0.5, 0)
  • glVertexPointer(2, GL_FLOAT, 0, vertices)
  • glEnableClientState(GL_VERTEX_ARRAY)
  • glColor4ub(255, 255,51, 128)
  • glDrawArrays(GL_TRIANGLE_STRIP, 0, 6)
  • glPopMatrix()
  • glDisable(GL_BLEND)

93
Exercise
  • Create an application that draws 2 shapes which
    will bounce off the sides of the screen and each
    other. Add a texture and vector physics to this
    shape if you need more of a challenge.
  • Create an application that draws stars in the top
    half of the screen.
  • Each star has a random rotation and location on
    the screen. The star will change a combination of
    size, color, transparency or rotation
    periodically to simulate twinkling.

94
Memory Management
  • If instance created by Object alloc message or
    malloc(size) C function it will not be
    automatically released. Each of these calls (as
    well as every time retain is called) increments
    the retain count.
  • Methods of releasing data (releasing the object
    reduces its retain count)
  • release ObjectVariable release
  • autorelease - ObjectVariable autorelease (if
    makes retain count 0, ensures object is not going
    to be referenced. Generally object is released at
    end of function/current scope)
  • dealloc() is called when objects retain count is
    0 (similar to C destructor)
  • Release all appropriate instance variables in
    dealloc

95
Objective-C Classes
  • Interface declared by _at_interface
    ClassNameSuperClass and ended by _at_end
  • SuperClass must always be Object or a subclass
    of Object (NSObject for the iPhone)
  • Implementation declared by _at_implementation
    ClassName
  • All class methods are public
  • Instance variables are private by default
  • Classes can be extended with categories (example
    later)

96
Objective-C
  • Used as default language in xcode
  • Can run almost all c programs
  • .h extension for header files and .m extension
    for implementation files
  • Methods in same class referenced using self
    (similar to the this keyword in c)
  • Syntax
  • Default return type is id
  • Message Object functionarg1 parameter2arg2
    (similar to Object-gtfunction(arg1, arg2))
  • -(returnType)function(type)arg1
    parameter2(type)arg2 code
  • (returnType)staticFunction(type)arg1
    parameter2(type)arg2 code

97
Extending Classes
  • // A class extension
  • _at_interface tempClass (Category)
  • // New methods
  • (BOOL) tOrF
  • (void) doSomething
  • _at_end
  • _at_implementation tempClass(Category)
  • (BOOL) tOrF
  • (void) doSomething
  • _at_end

98
Creating an iPhone Project
  • File-gtNew Project
  • Options we will discuss
  • OpenGL ES Application
  • View-Based Application
  • Tab Bar Application

99
Example of Objective-C
  • - (void)viewDidLoad
  • super viewDidLoad
  • self fnWithInt

100
Objective-C
  • -(void)fnWithInt(int)i int2(int)j
  • Method name - fnWithIntint2
  • C can call Objective-C functions and Objective-C
    can call C functions.
  • self can not be used from C functions
  • Work around this by creating a global variable or
    passing type id

101
Objective-C Called by C
  • -(int)getInteger
  • return 5
  • int aFunction(id this2)
  • /print the value returned by the getInteger
    function of the object passed to the method/
  • printf("i", this2 getInteger)
  • return 10

102
Concepts/Directives
  • _at_synthesize - used to automatically create
    missing getters (cd) and setters (setCd)
    function for an instance variable
  • Synthesized variables must have an associated
    _at_property
  • ex) _at_property (retain) UIDevice cd
  • //creates 2 methods cd and setCd(UIDevice)n
    ewDevice
  • _at_synthesize cd
  • //set cd to the current device using the
    synthesized method
  • self setCdUIDevice currentDevice

103
_at_property
  • _at_property (parameters) type name
  • nonatomic - allows more than one thread to access
    a getter/setter at a time
  • atomic - only allow one thread to access a
    getter/setter at a time (default)
  • retain - setter should use retain in the
    variable assignment
  • (ex var1 var2 retain)
  • Used for objects
  • assign - setter will assign directly to the
    variable
  • Used for basic variable types
  • readonly - setter will not be generated
Write a Comment
User Comments (0)
About PowerShow.com