1 / 37

Objective-C Foundation

Objective-C Foundation. Lecture 2. Flows of Lecture 2. Before Lab Introduction to Objective-C Intrinsic Variable Flow Control Class vs. Object User-Defined Class Variables Declaration and Initialization Methods Declaration and Invocation Objective-C String and Array During Lab

woods
Download Presentation

Objective-C Foundation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Objective-C Foundation Lecture 2

  2. Flows of Lecture 2 • Before Lab • Introduction to Objective-C • Intrinsic Variable • Flow Control • Class vs. Object • User-Defined Class • Variables Declaration and Initialization • Methods Declaration and Invocation • Objective-C String and Array • During Lab • Programmatically Creating and Managing UI Objects • Common properties of UI components • Animation – NSTimer • Removal of UI Objects

  3. Introduction to Objective-C • Objective-C is the official programming language used for developing iPhone apps • Objective-C is the superset of C • Just like the case of C/C++ • Everything that works in C also works in Objective-C • Implication: • You can use a C programming style to develop iPhoneapps, although it is not formal

  4. Intrinsic variable types we use in C/C++ also work in Objective-C Below are some of the supported intrinsic variables types Intrinsic Variable Types 4

  5. Flow Control – Conditional Statement • Objective-C uses exactly the same syntax as that of C/C++ in conditional statements • Example: int a = 0; int b = 1; if (a < b){ // Do something here } else{ // Do another thing here } 5

  6. Flow Control – Loop Control • Objective-C uses exactly the same syntax as that of C/C++ in for-loop and while-loop statements • For-loop example: for (int i = 0; i < b; i++){ // Do something here } • While-loop example: while (a < b){ // Do something here } 6

  7. Class vs. Object A class is a template / prototype of certain kind of objects Basically, it is a type (compared with int, float …) Defines the characteristics of objects that belong to the class Defines the fields (member variables) that the object has Defines the things that the object can do (methods) An object is an instance of a class Each object has its own copy of its member variables Each object can respond to invocation of its methods Many objects can be created from the same class

  8. Class vs. Object Conceptual Example – Room and CYC603 • Class • Room • Members • Light (is on or off) • No. of Chairs • No. of Tables • Projector status (on or off) • No. of Computers • Methods • Move in/Out Chairs/Tables/Computers • Turn on/off the light • Turn on/off the projector • Object • CYC103 EEE Computer Lab (An instance of Room Class) • Members • Light = on • No. of Chairs = 40+ • No. of Tables = 10+ • Projector = on • No. of Computers = 10+

  9. Objective-C Class Example – GUI-Specific Variable Types We need GUI-specific variable type (class) for displaying the UI components on the screen when creating a GUI application Below are all those we are using in the project

  10. For example, in a GUI application, a given image view on the screen is an object (e.g. the image, i.e., life1Image) It belongs to class UIImageView It has its state: The image is currently hidden or not It has its behaviour: When receiving a method call (say, telling it to change the hidden status), it will respond to the caller by changing its own hidden status e.g., life1Image.hidden = YES life1Image.hidden = No Objective-C Object Example I – life1Image

  11. Objective-C Object Example II – scoreLabel For example, in a GUI application, a given text label on the screen is an object (e.g., the “score” label) It belongs to class UILabel It has its state: The text shown on the label It has its behaviour: When receiving a method call (say, telling it to change the text), it will respond to the caller by changing its own label

  12. User-Defined Class • In this project, what we are doing is implementing the class – ViewController • As practised in previous lab session, we have to do: • In header file (.h file) • Variables Declaration • Methods Declaration • In Implementation file (.m file) • Corresponding methods implementation

  13. Variables Declaration and Initialization • Intrinsic type declaration and initialization, e.g., • int score = 0; • float timeElapsed = 0.0f; • Class type declaration; • UILabel* scoreLabel = [UILabelalloc]; • VolcanoRock * aRock = [VolcanoRockalloc]; (VolcanoRock is another user-defined class that you will use later) “*” is known as pointer, which points to the memory location storing the newly created object (in objective-C, a new object is created by calling alloc).

  14. Methods Declaration • No Return Variable: • - (void) initializeData; • Has Return Variable • - (id)initVolcanoRock; • - (bool)isOutOfBoundary; • Has one input parameter; • - (void) initializeData: (int) variable1; • Has 1+ input paramaters; • - (void) initializeData: (int) variable1: (int) variable2; • - (void) initializeData: (int) variable1 withVar2: (int) variable2; • Note that Both are valid

  15. Methods Implementation • Each method should be implemented in between @implementation and @end pair • In implementing a method, we have to implement the codes in-between the braces after the method declaration - (void) initializeData{ // Implement some codes here }

  16. Methods Invocation • Within a method implementation, you may need to invoke another method. This is done by“[ <object /class name> <method name>]”. • Method invocation of the same object (e.g, invoke “initializeData” in “viewDidLoad” ) - (void) viewDidLoad{ // Implement some codes here [self intializeData]; } • Method invocation of the other object (e.g., invoke “initVolcanoRock”ofvolcanoRock in “initializeData” - (void) initializeData{ // Implement some codes here aRock = [volcanoRockinitVolcanoRock]: }

  17. Methods Invocation • Method invocation with one input parameter. Parameters are separated by “:” [manImagesetCenter: CGPointMake( DEFAULT_MANIMAGE_CENTER_X, DEFAULT_MANIMAGE_CENTER_Y)]; Note that manImage is an UIImageView object • We will discuss what CGPointis later

  18. Objective-C Variable Types Similar to C++ or Java, Objective-C also has its own list of variable types. Below are all those we are using in the project

  19. Objective-C String • Strings in Objective-C are defined with an “@” sign • e.g., @"Carson" • It differentiates Objective-C strings from C style strings • There are two types of strings: NSString and NSMutableString • A string of type NSString cannot be modified once it is created • A string of type NSMutableString can be modified • Here, we will cover NSString only Refer to documentation for NSMutableString • Creation of a NSString object: • e.g., NSString * carsonStr = @"Carson"; • carsonStr is a pointer to an NSString object which is dynamically created 19

  20. Objective-C String: NSString • Example:int age = 29; float score = 99.9; NSString * carsonStr = @"Carson";NSString * string1 = [NSStringstringWithFormat:@"%@ age %d, %@ score %.01f ", carsonStr, age, carsonStr, score];// NSLog is to output string to the ConsoleNSLog(string1); • The output on the console is: • Carson age 29, Carson score 99.9 20

  21. C Style Array To make an array holding elements of intrinsic variable type (e.g. int): We can simply use C/C++ style of creating array Example: intmyArray[100];myArray[0] = 555;myArray[1] = 666;

  22. Objective-C Array • To make an array holding objects of a class (e.g., aRock) • Create an array object of class NSMutableArray • Add the objects of the same class into the array object • Finding the size of the array is important when we want to traverse the whole array • We can do so by invoking the count method • e.g.[array count]; (return 3 in this case) aRock array object bRock cRock 22

  23. Objective-C Array: NSMutableArray I • By using NSMutableArray, we can dynamically modify the array elements by using the method addObject • NSMutableArray * array = [[NSMutableArrayalloc] initWithCapacity:1]; • VolcanoRock * aRock = [[VolcanoRockalloc] initVolcanoRock]; • VolcanoRock * bRock= [[VolcanoRockalloc] initVolcanoRock]; • [array addObject:aRock]; • [array addObject:bRock];

  24. Objective-C Array: NSMutableArray II • To refer to a particular element in an array. You need to use the method objectAtIndex • VolcanoRock * aRock = [array objectAtIndex:0];

  25. Objective-C Array: NSMutableArray III • When a particular element inside the array is no longer in use, we can remove the element from the array • Remove the element from the array • [array removeObjectAtIndex:i]; • All elements beyond index i will then be moved one position forward 25

  26. Summary of the Comparison Between C/C++ and Objective-C

  27. Summary of the Comparison Between C/C++ and Objective-C

  28. Common Updates on UI Components • Setting Text for UILabel Object • Tutorial 1 Last Section – scoreLabel, timeLabel • UI components usually contain a property known as hidden • e.g., component.hidden = YES implies that the UI component is invisible; setting it to NO make it visible • Tutorial 2 Section 2.1 – life1Image.hidden = YES • Changing the center position of UI components to a specified location on the screen view, e.g., • [component setCenter:CGPointMake(center_x, center_y)]; • Used in the VolcanoRock “move” method (Tutorial 2 Section 3.2) Center (center_x, center_y)

  29. Create and Display an Image Create a UIImageView variable and specify the image file it is displaying UIImageView *myImageView = [[UIImageViewalloc] initWithImage: [UIImageimageNamed:@”pic.jpg”]]; Specify the frame (size and location) [myImageView setFrame:CGRectMake(x, y, width, height)]; Show it on the screen [self.viewaddSubview:myImageView];

  30. Example – Adding Man Image on to the screen (Tutorial 2 Section 2.2) • Create a UIImageView object with the image named “man.jpg” • Declare the variable somewhere: • UIImageView * manImage; • Initialize the variable: (Recall Objective-C type of initializaton) • manImage = [[UIImageViewalloc] initWithImage:[UIImageimageNamed:@“man.jpg"]]; • Setting the boundary frame of the UIImageView object • [manImagesetFrame:CGRectMake(DEFAULT_MAN_IMAGE_ORIGIN_X, DEFAULT_MAN_IMAGE_ORIGIN_Y, DEFAULT_MAN_IMAGE_WIDTH, DEFAULT_MAN_IMAGE_HEIGHT)]; • Putting the UIImageView object to the view to display • [self.viewaddSubview:manImage];

  31. Screen View Layer Concept • The device has to follow a certain sequence order in drawing the view objects on the screen view. • Usually, the drawing order is following the order of adding the object to the view • For example, • object A is added to self.view (i.e., [self.viewaddSubview:objectA]) • Then, object B is added to self.view • Both object A and object B overlap in some places • In this case, object B will be on top of object A object B object A

  32. Initial Position of VolcanoRock (Tutorial 2 Section 2.3) aRock volcanoImage Very strange to have the VolcanoRock object (aRock) to shoot out at the current location

  33. Push VolcanoRock below VolcanoImage • Change the drawing order by: [self.viewinsertSubview:aRock belowSubview:volcanoImage]; aRock volcanoImage

  34. Algorithms in Animation • Move the image object continuously within a short period of time to give the illusion of animation • Periodically invoke a method “progress” • How? • NSTimer can be used to perform this function • In the method “progress” • Generate New VolcanoRock (if necessary) • Method “initVolcanoRock”in the VolcanoRock Class • Update the current position of the existing volcanoRock • Method “move”inVolcanoRock in VolcanoRock Class

  35. NSTimer Class • Allows a certain method to be activated repeatedly within a predefined period of time. • The finest scale possible is : 1/30 second • To initialize NSTimer: • timer = nil; • Reset the Timer • timer=[NSTimerscheduledTimerWithTimeInterval:DEFAULT_TIMER_RATE • Schedule the timer to activate after DEFAULT_TIMER_RATE seconds target:self • The current view controller will handle the timer activation selector:@selector(progress) • The method progress will be invoked when timer is activated userInfo:nil repeats:YES]; • The timer will be activated periodically

  36. Moving Out of Boundary (Tutorial 2 Section 3.4) • VolcanoRock falls outside of the screen view boundary should be removed • Method “isOutOfBoundary”inVolcanoRock Class • We will discuss more on moving out of the screen view boundary next lecture

  37. Removing an UI Object from the screen To remove an UI object being referenced by VolcanoRock * aRockfromthescreen: [aRockremoveFromSuperView];

More Related