220 likes | 414 Views
Don’t Panic!. CSE 391 Fall 2012 Tony Scarlatos. Create Your Xcode Project. Launch Xcode and choose File > New Project In the project window, under the iPhone OS label, choose Single View Application from the Application Category
E N D
Don’t Panic! CSE 391 Fall 2012 Tony Scarlatos
Create Your Xcode Project • Launch Xcode and choose File > New Project • In the project window, under the iPhone OS label, choose Single View Application from the Application Category • Save the project to your desktop with the name PanicButton.
Launch Interface Builder • In the PanicButton project window you will see a PanicButton folder. Click the file inside called Main.storyboard. This is an XML file that describes some parts of the user interface. • Interface Builder will launch in the workspace to the right and you should see at the top of the palette on the right you will have several icons – File, Help, Identity, Attributes, Size, and Connections. In the lower right there is an interface objects palette.
Add an interface element • From the Objects palette, drag a Button into the View window (which is your iPhone panel). This button is an instance of the UIButton class. • With the button highlighted, select the Attributes icon from the upper right palette, where you can edit various attributes of the button. Type “Don’t Panic!” in the Title field for the button, and press Enter. • Select File > Save. Click the Run button in the upper left corner of the project window, which launches the iPhone Simulator.
Event-driven programming • Types of events that the iPhone responds to: • The app launching • Tapping a button • Selecting a row from a table • Shaking the phone • Gesturing • Moving to a new location (detected by the onboard GPS) • Many more…
Define a method • In the PanicButton project window in Xcode, select the PanicButton folder, and click the ViewController.h file. In this header file you will define the instance variables and the methods your class will have. (You will write the code for your methods in the implementation file). • In the code window to the right, type the following in the line above the @end statement, and then save the file: -(IBAction)showColor1:(id)sender;
Method, continued #import <UIKit/UIKit.h> @interfaceViewController : UIViewController { } -(IBAction)showColor1:(id)sender; @end The first line of the code imports classes from the UIKit framework. The second line indicates the name of the class is ViewController, and it is a subclass of the UIViewController class. The line you inserted defines a method. IBAction links this method to Interface Builder, and tells it you will connect this method to an object. showColor1 is the name of the method.
Connecting a method to an object • Switch back to Interface Builder by clicking on the Main.storyboard file. To the left of the view window select the ViewController icon. • Select the Connections icon (indicated by an arrow) in the upper right. You’ll see the showColor1 method under Received Actions. Drag a line from the circle to the right of your method to the button in the View Window and choose Touch Down as the event in the pop-up window that appears. • Save the file.
Implementing code for your method • Click the ViewController.m file in the PanicButton folder. This is the implementation file. • Add the following lines of code just above the @end statement, save your file, and click the Run icon: -(IBAction)showColor1:(id)sender { self.view.backgroundColor = [UIColorredColor]; }
Assignment statement • The syntax is L-value=expression. We’ll look at the expression first. • The code inside the square brackets calls a method. You have a class or an object, a space, and a method (UIColor is the class, redColor is the method). • You can look up the documentation for the UIColor class. Go to Help > Documentation and type UIColor in the search field. Under Class Methods you will see redColor.
Assignment statement, continued • Now let’s look at the L-value. • self is a keyword that refers to the current object. The object was defined in the interface file, indicating that ViewController is a subclass of UIViewController and it inherits all its properties and methods. @interfaceViewController : UIViewController { } • You can look up the UIViewController’s properties and methods in the Documentation.
Assignment statement, continued • So self is the ViewController object, and view is the view the controller manages. The UIView object has a property named backgroundColor.
Add another button • In the Xcode header file, ViewController.h, define another method, showColor2. • In Interface Builder, add another button to your view with the title “OK, Panic!” and connect the showColor2 method to that button. • In the implementation file ViewController.m write the showColor2 method, using redColor. Save your file and click Run.
Wrapping up • Experiment with changing the background color, button attributes, etc. Save and run your file. • Select your PanicButton project folder in the Finder window. • From the utilities drop-down menu (the one with the gear icon) select Compress.