User:KillersKRL/iPhone

iPhone Links
iPhone Developer Center Bug Reporting iPhone OS Overview Tools for iPhone OS Development Learning Objective-C: A Primer Creating an iPhone Application Object-C 2.0 Programming Language iPhone OS Programming Guide iPhone Human Interface Guidelines Cocoa Fundamentals Guide Foundation Framework Reference UIKit Framework Reference Interface Builder User Guide View Controller Programming Guide Table View Programming Guide for iPhone OS Optional Table-View Configurations Closer Look at table-View Cells NSIndexPath UIKit Additions

Xcode Notes Notes
File extensions for Objective-C h - header files m - source code for Objective-C and C code mm - source code for Objective-C, C, and C++ code xib - Resources such as Views edited with Interface Builder plist - Information Property List - This is where you setup your project's information such as company info and product name.

UIApplication
1. After the app is launched it sends an applicationDidFinishLaunching of the delegate class.

UIWindow
1. Drawing surface (only one in most situations)

UIView
1. Provides content (many) 2. initWithCoder - called after being unarchived from the nib file. 3. drawRect - all custom drawing takes place. 4. setMultiTouchEnabled (false by default).

UITableView
1. Inherits from UIScrollView. 2. Represents a hierarchical view of data that can be drilled down to reveal detailed information. 3. Must have a delegate and a data source. 4. The data source mediates between the application's data model and the table view thru the use of '''UITableViewDataSource protocol (interface in C#). The following are the key methods of this protocol:''' -(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView - This represents how many sections the table view will have. -(NSArray*)sectionIndexTitlesForTableView:(UITableView*)tableView This array represents the index titles for each section that runs down vertically along the right side of the table view for quick selection. a, b, c, d, e, f...    -(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath This method represents a cell at indexPath and you are to return a UITableViewCell at that indexPath. Use the tableView.desqueueReusableCellWithIdentifier to reuse the one assigned an identifier. This is done by allocating one with cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease].    -(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section         This method is used to return the number of rows for a given section.     -(NSInteger)tableView:(UITableView*)tableView sectionForSectionIndexTitle:(NSString*)title atIndex:(NSInteger)index         Returns the index for the given section title.     -(NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section         This method returns the string that represents the title for the section at section index. 5.  The delegate manages the appearance and behavior of the table view thru the use of UITableViewDelegate (interface in C#).  The following are the key methods of this protocol..     (NSIndexPath *)tableView:(UITableView*)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath - Here to not select a row return nil 6.  Return value objects representing index paths using NSIndexPath. 7.  Creating a UITableView programatically but it is recommended to inherit from UITableViewController because it handles setting both the delegate and the datasource for you.  This is an example of plain style.  To create a table view in grouped style set initWithFrame:style:UITableViewStyleGrouped     //RootViewController.h    @interface RootViewController : UIViewController      {        UITableView *tableView;        NSArray *timeZoneNames;     }     @property (nonatomic, assign) UITableView *tableView;     @property (nonatomic, retain) NSArray *timeZoneNames;     @end     //RootViewController.m    - (void)loadView     {        tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];        tableView.autoresizeMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;        tableView.delegate = self;        tableView.dataSource = self;        [tableView reloadData]; //Call on the reloadData method of tableView.        self.view = tableView;        [tableView release];     }     - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView     {        //Number of sections is the number of region dictionaries        return [displayList count];     }     - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section     {        //Number of rows in the number of names in the region dictionary for the specified section        NSDictionary *regionDictionary = [displayList objectAtIndex:section];        NSArray *zonesForSection = [regionDictionary objectForKey:@"TimeZones"];        return [zonesForSection count];     }     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath     {        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];        if (cell == nil)        {           cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"MyIdentifier"] autorelease];        }        //Get the section index, and so the region dictionary for that section        NSDictionary *regionDictionary = [displayList objectAtIndex:indexPath.section];        NSArray *regionTimeZones = [regionDictionary objectForKey:@"TimeZones"];        //Set the cell's text to the name of the time zone at the row        cell.text = [[regionTimeZones objectAtIndex:indexPath.row] objectForKey:@"timeZoneLocaleName"];        return cell;     }

UITableViewCell
1. The UITableViewDelegate must return a cell object for each visible row that a table view displays. 2. If a cell is reusable the data source assigns the cell object a reuse identifier (an arbitrary string) when it creates the cell. The table view stores the cell object in an internal queue and the data source can access the cell object via static NSString* myIdentifier = @"MyIdentifier"; UITableViewCell cell = [tableView dequeueReusableObjectCellWithIdentifer:myIdentifier]; Configuring a UITableViewCell object with both image and text - (UITableViewCell*)tableView:(UITableView*)tv cellForRowAtIndexPath:(NSIndexPath*)indexPath {    UITableViewCell* cell = [tv dequeueReusableCellWithIdentifier:@"TextAndImage"]; if (cell == nil) {       cell = [UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"TextAndImage" autorelease];        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;     }     cell.text = [zodiacSigns objectAtIndex:[indexPath.row; NSString* imagePath = [[NSBundle mainBundle] pathForResource:[cell.text lowercaseString] ofType:@"png"];    UIImage* icon = [UIImage imageWithContentsOfFile:imagePath];     cell.image = icon;     return cell;   } Note: the table view sends a tableView:willDisplayCell:forRowAtIndexPath: message to its delegate just before it draws a row.  The delegate can change state-based properties such as selection and background color, and not content. 3.  Creating and using custom table-view cells.  When creating your own custom table-view cells use these design aspects and performance constraints.     a.  Avoid drawing the entire cell.     b.  Avoid transparency.  It impacts scrolling performance.     c.  Mark the cell as needing updating by calling the setNeedsDisplay when properties change so that the drawRect gets called.

UINavigationController
1. Manages a navigation bar that traverses a sequence of table views by maintaining a stack of view controllers. 2. One view controller for each of the table views displayed. 3. Is required to have at least one view controller on its stack. 4. Adding a Add button to the navigation bar. -(void)loadView {... self.addButton = [UIButton buttonWithType:UIButtonTypeNavigation]; // add Button is a view controller property NSString* addTitle = NSLocalizedString(@"+", "Add button title"); [addButton setTitle:addTitle forStates:(UIControlStateNormal & UIControlStateHighlighted)]; [addButton addTarget:self action:@selector(addItem:) forControlEvents:UIControlEventTouchUpInside]; self.navigationItem.customRightView = addButton;

UIViewController
1. Encapsulates properties for setting up its navigation item and the view to display when that view controller is the top view controller.

UIAccelerometer
1. Recieves touch events

CLLocationManager
1. Animations are built into the UIKit already. The following steps are the basic steps for animating objects: a. Call the beginAnimations:context: method of the view you want to animate. b. Configure animation properties. c. Call the commitAnimations.

View Controller Classes
NSObject UIResponder UIApplication UIView UITabBar UINavigationBar UINavigationItem UIBarItem UITabBarItem UIBarButtonItem UIViewController UITabBarController UITableViewController UINavigationController UIImagePickerController

Objective-C Notes
1. Strings: NSString* helloWorld = @"Hello World!"; NSString* anotherString = [NSString stringWithFormat:@"%d %@", 1, @"String"]; //Create an Objective-C string from a C string NSString* fromCString = [NSString stringWithCString:"A C string" encoding:NSASCIIStringEncoding]; 2. Class declaration: (MyClass.h) @interface MyClass : NSObject //Member variable declaration block. Not the class declaration block. {   int count; id data; NSString* name; } //Method declarations of the class - (id)initWithString:(NSString*)aName;                //- << instance method of a class + (MyClass*)createMyClassWithString:(NSString*)aName; //+ << static/class method //Properties of the class @property BOOL flag; @property (copy) NSString* nameObject; //Copy the object during assignment. @property (readonly) UIView* rootView; //Create only a getter method. @property (copy, readwrite) NSString* value; @end //End of the class declaration 3. Class implementation: (MyClass.m) import "MyClass.h" @implementation MyClass @synthesize value; // instructs the compiler to create accessor methods to match the specifications given in the declaration //id represents any type of object can be returned - (id) initWithString: (NSString*)aName { if (self = [super init]) { count 0; data = nil; name = [aName copy]; //copy a string return self; } + (MyClass*) createMyClassWithString: (NSString*)aName { MyClass* myClass = [[MyClass alloc]initWithString:aName];   return myClass; } 4. Protocol declaration - (Interface in C#) @protocol MyProtocol - (void)myProtocolMethod; @end;

Interface Builder
1. Interface objects - are the objects that are actually created when the document is loaded. 2. Proxy objects - are objects that are tied but live outside of the document. 3. Views and Controls - Everything onscreen descends from the UIView class. a. Toolbar Buttons - The handler method your target object uses to receive the message must have the following format: - (IBAction)myActionHandler:(id)sender; The bar button item has a single selector action that you can connect to the action method of the designated target object. 4. Controller Objects - Any non-visual object that descends from NSObject can be placed in a Nib file. a. View Controllers - The most common way to use a view controller is as the File's Owner of a nib file, but can also instantiate them inside your nib file as needed. b. Using a View Controller as File's Owner of a Nib File: 1. Create new nib file using Cocoa Touch View template and configure as follows: a. Set the class of the File's Owner proxy to the class of your custom view controller. b. Connect the view outlet of the File's Owner to the view provided by the template. 2. Open the view and add any additional subviews to it to define your user interface. 3. Connect any additional outlets and actions. c. Tab Bars - These provide a way to manage large amounts of information with buttons in a tab bar. Tab Bar Controller                    - UITabBarController Tab Bar                            - UITabBar View Controller (Item 1)           - UIViewController Tab Bar Item (Item 1)           - UITabBarItem Selected View Controller (Item 2)  - UIViewController Tab Bar Item (Item 2)           - UITabBarItem Note: You must add the tab bar controller's view and add it as a subview to your window. - (void)applicationDidFinishLaunching:(UIApplication *)application {             [window addSubView:[myTabBarController view]]; }        Note: If you have more than five items do not create a More button. Instead configure the customizableViewControllers property of the tab bar controller programmatically and it will do it automatically. Configure the root view for a tab, do the following: 1. Create a new nib file and configure as described in Using a View Controller as File's Owner of a Nib File 2. In the nib file containing your tab bar controller object, select the view controller for the appropriate tab item. 3. Open the Attributes inspector. 4. In the Nib Name field of the inspector, enter the name of the nib file (without any file extension) you just created. d. Navigation Bars 1. Is a visual element used in conjunction with a navigation controller object to navigate complex data hierarchies. 2. Displays the current location in the navigation hierarchy and provides an optional button for navigating back and an edit button for manipulating data at the current level 3. Manages the view controllers that coordinate each screen's content view. Navigation Controller                    - UINavigationController Navigation Bar                        - UINavigationBar View Controller (Navigation Item)     - UIViewController Navigation Item (Navigation Item)  - UINavigationItem Important Note: A navigation controller object can have only two child objects: a navigation bar (UINavigationBar) and a view controller (UIViewController) for managing its root view. Both of these objects must be present in order to initializing your navigation controller properly at runtime. Note: You must add the navigation controller's view and add it as a subview to your window. - (void)applicationDidFinishLaunching:(UIApplication *)application {               [window addSubView:[myNavBarController view]]; }