1 / 32

ART40545

ART40545. Basics of Programming:IOS Kris Secor. Week 2. Who is Wayne Dobson ?. First Responder…. The most prominent UIElement interacting with a user at a given time Let’s remove that keyboard! Now let’s add a background button. Delegate. for in loop.

maik
Download Presentation

ART40545

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. ART40545 Basics of Programming:IOS Kris Secor IOS Development

  2. Week 2 • Who is Wayne Dobson? IOS Development

  3. First Responder…. • The most prominent UIElement interacting with a user at a given time • Let’s remove that keyboard! • Now let’s add a background button IOS Development

  4. Delegate IOS Development

  5. for in loop • Introduced in Objective-C 2.0 (“fast enumeration”) for(Item_Type *item in Collection_of_Items) { //do whatever with the item Nslog(@” Looking now at %@”, item); } Note: %@ in the NSLogconverts whatever is passed (in this case item) to a string

  6. C-style arrays int multipleValues[5]; multipleValues[0] = 50; multipleValues[1] = 60; or int multipleValues[5] = {50,60,70,80,90}; or int multipleValues[0] = {50,60,70,80,90}; IOS Development

  7. C-Style Arrays with Objects Formal: NSString *stringArray[5]; stringArray[0] = [[NSStringalloc] initwithString:@”Hello”]; stringArray[0] release; Informal (without memory); NSString *stringArray[5] = {@”first”,@”second”,@”third”,@”fourth”,@”fifth”}; NSLog (@”The first member is %@”,stringArray[0]); • //no bounds checking • //can’t mix types • //fixed size IOS Development

  8. Array Objects and Methods inti; NSMutableArray *array = [[NSMutableArrayalloc]init]; for (i=0;i<10;++i){ [array addObject:[NSStringstringWithFormat:@"%d", (i*3)]]; } inty=0; for (id obj in array){ NSLog(@"array element %i is %@",y,obj); y++; } IOS Development

  9. Functions --- pass by reference void myFunction(void) { for ( inti = 1 ; i < 5000 ; i++ ) { if (i % 5 == 0) { continue; // jump back to the top. } NSLog(@"The value of the index is %i", i); } } int main (intargc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePoolalloc] init]; // call the function myFunction(); [pool drain]; return 0; }

  10. Passing Arguments void myFunction(intx, inty) { NSLog(@"The value of the x is %i and the value of y is %i",x,y); } int main (intargc, const char * argv[]){ // call the function myFunction(4,5); return 0; } IOS Development

  11. Pointers and Objects NSMutableString *message = @"Hello"; NSString *newmessage = [message insertString:@" Kris, how are you?" atIndext:3]; NSLog(@"The new message is @%",newmessage); IOS Development

  12. Passing the pointer Void myFunction (NSString *inbound) {NSLog(@”The message was %@ “,inbound); } NSString *message = @”Hello”; myFunction(message); Let’s look at some basics and run it: Code Break! IOS Development

  13. Classes • Have both definition file (interface) and implementation file : classname.h and classname.m • Similar to how have .h and .cpp in C++

  14. Declaring a class in ClassName.h #import <Cocoa/Cocoa.h> @interface ClassName : Parent { //class variables int age; NSString name; } // methods declared -(void)setAge:(int)number; -(void)setName:(NSString)n; -(int)getAge; -(NSString)getName; @end #import <standardimports.h> #import “local-your-otherfiles.h” @interface ClassName: Parent { //class variables } //methods -(return_type) methodName:(type)param1, (type) param2; @end

  15. Declaring methods C++ syntax void function(int x, int y, char z); Object.function(x, y, z); Objective-C syntax -(void) method:(int)x, (int)y, (char)z; [Object function:x, y, z]; -(return type) function_name: (type) p1, (type) p2, ***; Apply function to Objectpassing parameters x,y,z

  16. Whats this + and – stuff? • When declaring or implementing functions for a class, they must begin with a + or - • + indicates a “class method” that can only be used by the class itself. (they are like static methods in Java invoked on class itself) • - indicates “instance methods” to be used by the client program (public functions) –invoked on an object / class instance . (they are like regular methods in Java invoked on object)

  17. Inheritance • Root class is NSObject • Inheritance is cumulative. A Square object has the methods and instance variables defined for Rectangle, Shape, Graphic, and NSObject, as well as those defined specifically for Square

  18. Inheritance (cont.) • Instance Variables: The new object contains not only the instance variables that were defined for its class, but also the instance variables defined for its super class, all the way back to the root class • Methods: An object has access not only to the methods that were defined for its class, but also to methods defined for its super class • Method Overriding: Implement a new method with the same name as one defined in a class farther up the hierarchy. The new method overrides the original; instances of the new class will perform it rather than the original

  19. The Interface • The declaration of a class interface begins with the compiler directive @interface and ends with the directive @end @interface ClassName:ItsSuperclass { instance variable declarations } method declarations @end

  20. Frameworks • UIKit.framework for developing standard iOS GUIs (buttons etc.) • UITextField (user-entered text that brings up the keyboard) • UIFont • UIView • UITableView • UIImageView • UIImage • UIButton (a click-able button) • UILabel (a string of text on-screen) • UIWindow (main Window on iPhone)

  21. iOS programming • Event driven framework • Interface Designer has some extra macros for the code that act like hooks to variables; • IBAction - trigger events like mouse clicks • IBOutlet - captures outputs from events • These tags are not compiled (don't affect the code) but sit as an extra before variables that the Interface Designer can see.

  22. Start a New XCode iOS Project • New Project → iPhone/iPad OS • Navigation-Based - “navigation controller” as used in Contacts app. • OpenGL ES - cut-down OpenGL • Tab Bar – app in style of iPod app. • Utility – Flipside app in style of stock quote app. • View-Based – single view that you draw into and display to screen. • Window-Based – basic app with a main window. (we will use this one for examples.)

  23. Some Xcode items explored • An outlet defines a path between the code and the view that can be used to read and write values. • An action defines a method in your application that can be triggered via an event within a view, such as a touch or swipe. IOS Development

  24. @property • The @property directive declares elements in a class that should be exposed via "getters" and "setters" (or accessors and mutators, if you prefer). Properties are defined with a series of attributes, most frequently nonatomic and retain on the iPhone. IOS Development

  25. @synthesize • The @synthesize directive creates simplified getters and setters, making retrieving and setting values of an object very simple. • @synthesize myLabel; • @synthesize firstNum; • @synthesize secNum; IOS Development

  26. -(IBAction)doCalculation:(id)sender; • -(IBAction)doCalculation:(id)sender; • Notice that the declaration includes a sender parameter with the type of id. This is a generic type that can be used when you don't know (or need to know) the type of object you'll be working with. By using id, you can write code that doesn't tie itself to a specific class, making it easier to adapt to different situations. • When creating a method that will be used as an action (like our doCalculation example), you can identify and interact with the object that invoked the action through the sendervariable (or whatever you decide to call it in your code). This will be handy if you decide to design a method that handles multiple different events, such as button presses from several different buttons. IOS Development

  27. Let’s Plan Our Calculator App • What will we need? • Comment it out? • What will be out properties? • What will be our actions? IOS Development

  28. Your Projects… • Intro to story boards • What Initial Layout will you choose? • What elements will give the best usability considering the interface? Will gestures or accelerometers be necessary? IOS Development

  29. Frameworks Needed • For UI Stuff: #import <UIKit/UIKit.h> • For Basic Code Stuff: #import <Foundation/Foundation.h> • For Graphics • For GeoLocation#import <CoreLocation/CoreLocation.h> or #import <MapKit/MapKit.h> IOS Development

  30. Apps in class… • We will do an App that stores something several different ways plist/sqlite/coredata • We may elect to use the latest icloud technology to store things • we will do a geo location app • we will do a multimedia app • All of these apps will have homework that extend on them and you are free to use all code for your projects… IOS Development

  31. For Multimedia: • #import <MediaPlayer/MediaPlayer.h> • #import <AVFoundation/AVFoundation.h> • #import <CoreAudio/CoreAudioTypes.h> • #import <CoreImage/CoreImage.h> IOS Development

  32. Others we may need • #import <AddressBook/AddressBook.h> • #import <AddressBookUI/AddressBookUI.h> • #import <MessageUI/MessageUI.h> • #import <Social/Social.h> • #import <CoreMotion/CoreMotion.h> IOS Development

More Related