IPhone SDK
From Robertjd
Media Player Framework
Really good examples how how to use the media player framework:
http://oleb.net/blog/2009/07/the-music-player-framework-in-the-iphone-sdk/
Cocoa Specific
String programming guide
http://developer.apple.com/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html
Notifications
- http://developer.apple.com/iPhone/library/documentation/Cocoa/Conceptual/Notifications/Introduction/introNotifications.html#//apple_ref/doc/uid/10000043
- http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/Reference/Reference.html
- http://hackertoys.com/2008/10/01/nsnotificationcenter/
Posting a notification:
[[NSNotificationCenter defaultCenter] postNotificationName: @"keyboardDone" object: nil];
Observing a notification:
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(keyboardDoneObserver:) name: @"keyboardDone" object: nil];
Handling a notification:
-(void)keyboardDoneObserver: (NSNotification*)notification { // do work here }
Objective C Specific
Bool, disaster:
http://iphonedevelopertips.com/objective-c/of-bool-and-yes.html
Class type checking
if([sender isKindOfClass:[UIBarButtonItem class]]){ //stuff }
Convert to a string
NSString *string = [NSString stringWithFormat:@"%d", yourInteger];
http://www.idevgames.com/forum/showthread.php?t=9273
Compare Strings
if ([string1 isEqualToString: string2]) { //do something }
Date/Time formatting
See here for a useful date/time formatting function:
iPhone Simulator
Filesystem
Is here:
/Users/<you>/Library/Application Support/iPhone Simulator/User/Applications/<APP GUID>/Documents
Interface Builder
Access UI objects created in IB using tags
Keyboard shortcuts
- Command-T for the Fonts panel.
Subclassing
Make sure to create an instance of your subclass! You can't create a generic subclass and expect it to have what you need from your custom class. duh: http://forums.macrumors.com/showpost.php?p=7327494&postcount=10
Access the application delegate
[[UIApplication sharedApplication] delegate];
+ (iLaughAppDelegate *)singleton { iLaughAppDelegate *delegate = (iLaughAppDelegate *)[[UIApplication sharedApplication] delegate]; return delegate; }
Data and Resource Management
Check that a file exists
Do this with NSFileManager:
NSFileManager *fileManager = [NSFileManager defaultManager]; if([fileManager fileExistsAtPath:fileNameString){ return YES; }else{ return NO; }
Check for Nib
if([[[NSBundle mainBundle] autorelease] pathForResource:fileName ofType:@"nib"] != nil) { //file found }
Path to a resource
NSString *thePList = [[NSBundle mainBundle] pathForResource:@"RouteList" ofType:@"plist"]; //Change YourInfo to the name of the plist file
Logging
http://iphoneincubator.com/blog/tag/nslog
http://discussions.apple.com/thread.jspa?threadID=1462712
Troubleshooting
Model view controller does not appear
I was trying to do a pushModalViewController from within viewDidLoad on a UIViewController object. This wasn't working. I then setup an IBAction method in the UIViewController object which would push the modal view controller. That worked.
The Developer Disk Image could not be mounted
An error in the organizer that occurs when the OS on the iPhone device is newer than your SDK version
Request for member currentLocation in something not a structure or union
For reasons I have yet to investigate, doing an include instead of a @class declaration solves this problem:
Unrecognized selector
An error looking like this:
2009-06-05 10:20:19.496 MuniReport[5683:20b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UIViewController setBusType:]: unrecognized selector sent to instance 0xf52140'
Generally means "method not found in class", which usually happens when you create something in Interface Builder and don't set the appropriate class for it. See this post here:
http://forums.macnn.com/79/developer-center/370199/unrecognized-selector-sent-to-instance/
Whereas one like this:
2009-08-23 13:26:09.254 MuniReport[30664:20b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[Report reportImage]: unrecognized selector sent to instance 0xd2d020'
Probably means that you forgot to synthesize the property called reportImage
Keyboards
No return button for number pad keyboard:
http://discussions.apple.com/thread.jspa?messageID=7530144&tstart=0
CSS hack for defining keyboard type:
http://www.bennadel.com/blog/1197-Defaulting-To-The-Numeric-Keyboard-On-The-iPhone.htm
UIActionSheet
A quick example:
-(IBAction) getPicture{ UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:@"Get image from where?" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Take new picture",@"Get from iPhoto Library",nil]; [menu showInView:self.view]; [menu release]; return; }
Note that you can declare "nil" if you don't need a particular button.
When using with a Tab Controller
You have to add this to the window of the view if you are adding the UIActionSheet within a Tab Controller, like so:
[menu showInView:[self.view window]];
Otherwise the clickable areas for the buttons will be offset from the buttons. See here for more info:
http://discussions.apple.com/thread.jspa?threadID=1800450
A longer UIActionSheet example with delegate methods:
UIColor
A handy macro for setting color:
#define RGB(r, g, b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1] msgLabel.textColor = RGB(255, 251, 204);
Taken from: http://iphonedevelopertips.com/cocoa/uicolor-macros.html
UILabel
Hide/Unhide
[myLabel setHidden:YES]; [myLabel setHidden:NO];
UIImagePickerController
An example of how to pick images from the iPhoto library:
http://trailsinthesand.com/picking-images-with-the-iphone-sdk-uiimagepickercontroller/
I was hoping for a way to access the user's camera roll programmatically through some kind of class, but that doesn't seem to be possible right now:
http://stackoverflow.com/questions/613324/iphone-custom-uiimagepickercontroller
UINavigationController
Better traverse the navigation controller stack:
UITextField
Change the text field color
textField.textColor = [UIColor blackColor];
Max field length
Specify the following function in the class of the UIViewController which contains the text field, and then set the text field's delegate to be the File Owner of the view controller:
#define MAX_LENGTH 4 - (BOOL)textField: (UITextField *)textField shouldChangeCharactersInRange: (NSRange)range replacementString: (NSString *)string { if (textField.text.length >= MAX_LENGTH && range.length == 0) { return NO; } else { return YES; } }
See also: http://blog.1530technologies.com/2009/04/iphone-programing-max-length-text-field.html