This appendix contains the source code for the Hello, World! application described in “Tutorial: Hello, World!.”
Listing A-1 main.m
// main.m |
#import <UIKit/UIKit.h> |
int main(int argc, char *argv[]) { |
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; |
int retVal = UIApplicationMain(argc, argv, nil, nil); |
[pool release]; |
return retVal; |
} |
Listing A-2 HelloWorldAppDelegate.h
// HelloWorldAppDelegate.h |
#import <UIKit/UIKit.h> |
@interface HelloWorldAppDelegate : NSObject <UIApplicationDelegate> { |
UIWindow *window; |
} |
@property (nonatomic, retain) IBOutlet UIWindow *window; |
@end |
Listing A-3 HelloWorldAppDelegate.m
// HelloWorldAppDelegate.m |
#import "HelloWorldAppDelegate.h" |
#import "MyView.h" |
@implementation HelloWorldAppDelegate |
@synthesize window; |
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { |
// Override point for customization after application launch. |
MyView *view = [[MyView alloc] initWithFrame:[window frame]]; |
[window addSubview:view]; |
[view release]; |
[window makeKeyAndVisible]; |
return YES; |
} |
- (void)dealloc { |
[window release]; |
[super dealloc]; |
} |
@end |
Listing A-4 MyView.h
// MyView.h |
#import <UIKit/UIKit.h> |
@interface MyView : UIView { |
} |
@end |
Listing A-5 MyView.m
// MyView.m |
#import "MyView.h" |
@implementation MyView |
- (id)initWithFrame:(CGRect)frame { |
if (self = [super initWithFrame:frame]) { |
// Initialization code |
} |
return self; |
} |
- (void)drawRect:(CGRect)rect { |
NSString *hello = @"Hello, World!"; |
CGPoint location = CGPointMake(10, 20); |
UIFont *font = [UIFont systemFontOfSize:24]; |
[[UIColor whiteColor] set]; |
[hello drawAtPoint:location withFont:font]; |
} |
- (void)dealloc { |
[super dealloc]; |
} |
@end |
Last updated: 2010-07-02