130 likes | 236 Views
Basic Interaction. From Beginning iOS 4 Development (Chapter 3 and 4) and The iPhone Developer’s Cookbook (Chapter 2). iPhone Application Skeleton. Main.m Creates primary autorelease pool Invokes application event loop. MyAppDelegate.h MyAppDelegate.m
E N D
Basic Interaction From Beginning iOS 4 Development (Chapter 3 and 4) and The iPhone Developer’s Cookbook (Chapter 2)
iPhone Application Skeleton • Main.m • Creates primary autorelease pool • Invokes application event loop • MyAppDelegate.hMyAppDelegate.m • Sets how program react at critical points • Initialize window system at start • Wrap up at termination • Handles memory warnings • MyViewController.hMyViewController.m and MyViewController.xib • Defines your application • How application responds to events • What objects are used in the application
Sandboxes • Security feature • Limits application’s access to file system, network resources and hardware • Characteristics • May not access any other programs sandbox • May not share data • May not read or write to files outside of sandbox • You own your own library, document and /tmp folders
Model – View - Controller • Known as MVC • Separates how object looks from how it behaves • Enables independent updates of program components • Model –classes that hold application data • View –windows, controls and user interactive elements • Controller – binds model and view together
Outlet Pointer that points to an object Used to make changes to an object Created in Xcode Linked in Interface Builder Example: coding a change of value in a display label. Need an outlet to connect to the label to make coded changes.
Creating an Outlet • Declare in the .h file (*ViewController.h) Add UILabel *statusText; to @interface Add @property and the action • Implement in the implementation file or .m file (*ViewController.m) Add @synthasize (to tell compiler to create assessor and mutator methods) Add action code Dealloc
Accessors and Mutators Also known as getters and setters Pairs of methods used to set and retrieve values for each of a class’s instance variables. Now using @property in the .h file and @synthasize in the .m file replaces the methods for creating getters and setters.
Application Delegates • Classes that handle things on behalf of another object • UIApplication class—iPhone has one and only one UIApplication • Handles the application loop and routing input to the right controller class • Works behind the scenes
Connecting Outlets • Must create outlet and save in Xcode before connecting • In Interface Builder, after adding object(s) to the view • Hold down control key, click on File’s Owner and drag to object to be linked in View. • Release and will see a list of the outlets you have created • Select the appropriate outlet
Adding Actions Open connections inspector (2) Look under Events and select the event you wish to link Click circle next to event and drag to File’s Owner Icon and select the pre-coded method (created earlier in Xcode)
Active, Static and Passive controls • Active—interactive—does something when interacted with (example button) • Static—may change programmatically, but the user can not interact with directly (example label) • Passive—interactive—but does not trigger an action (example text field) Many of the controls may be used in any of the preceding ways All are subclasses of UIControl and are capable of having actions. Most may be used passively, or made inactive (static) when created or at runtime.
Behavioral differences between Mac and iPhone controls • iPhone controls may trigger multiple actions depending on how touched, Mac controls usually have one action that is triggered. (click) • iPhone has no physical keyboard—just a view filled with a series of button controls.
Some Useful shortcuts in Interface Builder 1 –property Inspector 2 –connections Inspector 4 –identity Inspector = -to size to fit