1 / 103

iPhone Programming

Version 2 Created by Nathan Magnus. iPhone Programming. Apple. Your most useful resource is the Apple developer documentation. http://developer.apple.com/. Xcode IDE. Apple provides an IDE for creating iPhone applications Xcode can also be used to create OSX applications. Xcode IDE.

matia
Download Presentation

iPhone Programming

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. Version 2 Created by Nathan Magnus iPhone Programming

  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/index.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 -> 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-> 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 “@interface webNavViewController : UIViewController {“ • Add “IBOutletUIWebView *webView;” • Under “}” before “@end” • Add “@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 -> Connections Inspector)

  16. Program 1 • Click on the circle beside “New Referencing Outlet” and drag to “File’s 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 [superviewDidLoad]; • Create a NSURL • NSURL *url = [NSURL URLWithString:@“http://www.google.ca”]; • The “@” makes the following string a NSString • Create a NSURLRequest • NSURLRequest *request = [NSURLRequestrequestwithURL:url]; • Load the request • [webViewloadRequest:request];

  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->Attributes 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 group’s 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->Connections Inspector) • Select each object on the interface builder (UITextField, UIButton, UIImageView, UILabel), drag to “File’s Owner” and select the appropriate option • For the UITextField, click on the circle beside “delegate” and drag to “File’s Owner”

  34. Program 2

  35. Program 2 • Add to BoomViewController.h • After “@interface BoomViewController : UIViewController {“ • IBOutletUILabel *label; • IBOutletUITextField *input; • IBOutletUIImageView *image; • IBOutletUIButton *button;

  36. Program 2 • After “}” before “@end” • @property (nonatomic, retain) UILabel *label; • @property (nonatomic, retain) UITextField *input; • @property (nonatomic, retain) UIImageView *image; • @property (nonatomic, retain) UIButton *button; • -(BOOL)textFieldShouldReturn: (UITextField*)theTextField; • -(void)checkInput;

  37. Program 2

  38. Program 2 • In BoomViewController.m • Uncomment the “–(void)viewDidLoad” method • Add after “[superviewDidLoad];” • [labelsetText:@“Enter the detonation code.”]; • [buttonaddTarget:selfaction:@selector(checkInput) forcontrolEvents:UIControlEventTouchUpInside];

  39. Program 2

  40. Program 2 • Add after the “–(void)viewDidLoad” function • -(BOOL)textFieldShouldReturn: (UITextField*)theTextField { • [theTextField resignFirstResponder]; • return YES; • }

  41. Program 2

  42. Program 2 • Add after the “-(void)viewDidLoad” method • -(void)checkInput { • NSString *userInput = [inputtext]; • if([userInput compare:@“Boom!”]= =NSOrderedSame) • { • [labelsetText:@“Your instructor went BOOM!”]; • [image setImage:[UIImageimageNamed:@“explosion.jpg”]]; • } • else • { • [labelsetText:[NSString stringWithCString:“That is not the correct detonation code.”]]; • [imagesetImage:[UIImageimageNamed:@“nathan.jpg”]]; • } • }

  43. Program 2

  44. Finished Product: Program 2

  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 } elseif(condition) { code } else (condition) { code } switch (condition) { case constant: code break; default break; } Comparison Operators

  48. Objective-C Loops: do { code } while(condition); while(condition) { code } for(initialize; condition; increment) { code } Arrays: Declaration: type name[sizeOfArray]; Initialize: type name[] = {value1, value2 name[0]=value1; name[1]=value2; Pointers: Objects when using [Objectmethod]; 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 { constunsigned loopAmt = 10; printf("Testing: "); for(unsigned i=0; i<loopAmt; i++) printf("%i...", i); //prints “Testing:0…1…2…3…4…5…6…7…8…9… }

  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

More Related