Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Beginning iPhone Application Development

Beginning iPhone Application Development

[OUT OF DATE]

iPhone 程式開發新手入門
2010/12/02 @ 師大資工系 C209 教室

採 CC BY-NC 3.0 授權,歡迎散發 :)

-
Beginning iPhone Application Development
2011/12/14 @ Room C209, Dept. of CSIE, NTNU

Feel free to distribute under CC BY-NC 3.0 :)

Dan Chen

May 02, 2012
Tweet

More Decks by Dan Chen

Other Decks in Programming

Transcript

  1. • – – • – • – – • December

    2, 2010 S.C. Chen @ VIPLab 11
  2. December 2, 2010 S.C. Chen @ VIPLab 12 main() main.m

    MyAppDelegate.h MyAppDelegate.m MyViewController.h MyViewController.m MyViewController.xib
  3. December 2, 2010 S.C. Chen @ VIPLab 13 int main

    (int agrc, char* argv[]) { NSAutoReleasePool *pool = [[NSAutoReleasePool alloc] init]; int retval = UIApplication( argc, argv, nil, @“MyAppDelegate"); [pool release]; return retval; }
  4. December 2, 2010 S.C. Chen @ VIPLab 14 int main

    (int agrc, char* argv[]) { NSAutoReleasePool *pool = [[NSAutoReleasePool alloc] init]; int retval = UIApplication( argc, argv, nil, @“MyAppDelegate"); [pool release]; return retval; }
  5. December 2, 2010 S.C. Chen @ VIPLab 15 int main

    (int agrc, char* argv[]) { NSAutoReleasePool *pool = [[NSAutoReleasePool alloc] init]; int retval = UIApplication( argc, argv, nil, @“MyAppDelegate"); [pool release]; return retval; }
  6. • – • – myobj->setA(3); – [myobj setA: 3]; •

    • December 2, 2010 S.C. Chen @ VIPLab 17
  7. • – @ @"Foo“ – printf("%s %@", "Foo", @"Bar"); •

    – NSArray *colors = [NSArray arrayWithObjects: @"Black", @"Gray", nil]; for (NSString *color in colors) printf("a %@ car\n", color); December 2, 2010 S.C. Chen @ VIPLab 18
  8. • new delete – –  • – • BOOL

    TRUE YES FALSE NO December 2, 2010 S.C. Chen @ VIPLab 19
  9. @interface December 2, 2010 S.C. Chen @ VIPLab 21 #import

    <Foundation/Foundation> @interface Car : NSObject { int year; NSString *make; NSString *model; } - (void) setMake:(NSString *) aMake andModel:(NSString *) aModel andYear:(int) Year; - (void) printCarInfo; - (int) year; @end
  10. December 2, 2010 S.C. Chen @ VIPLab 22 • void

    setProperties(char *c1, char *c2, int i); myCar->setProperties("Foo", "Bar", 5566); • - (void) setMake:(NSString *) aMake andModel:(NSString *) aModel andYear:(int) year; [myCar setMake:@"Foo" andModel:@"Bar", andYear:5566]; setProperties() setMake:andModel:andYear:
  11. December 2, 2010 S.C. Chen @ VIPLab 23 NSArray *anotherArray

    = [NSMutableArray array]; // addObject: is a mutable-only method [anotherArray addObject: @"Hello World"]; 1) NSArray *myArray = [NSArray array]; 2) NSMutableArray *mutableArray; 3) mutableArray = myArray; 4) [mutableArray addObject: @"Hello World"]; Produces a compile-time warning, but works fine Line #3 produces a compile-time warning, Line #4 will BOMB at run-time
  12. @implementation December 2, 2010 S.C. Chen @ VIPLab 24 #import

    "Car.h" @implementation - (id) init { self = [super init]; if (!self) return nil; make = model = nil, year = 1901; return self; } - (void) setMake:(NSString *) aMake andModel: (NSString *) aModel andYear:(int) year { make = [NSString stringWithString: aMake]; model = [NSString stringWithString aModel]; year = aYear; }
  13. @implementation December 2, 2010 S.C. Chen @ VIPLab 25 #import

    "Car.h" @implementation - (id) init { self = [super init]; if (!self) return nil; make = model = nil, year = 1901; return self; } - (void) setMake:(NSString *) aMake andModel: (NSString *) aModel andYear:(int) year { make = [NSString stringWithString: aMake]; model = [NSString stringWithString aModel]; year = aYear; }
  14. • – • Car *myCar = [Car car]; • [myCar

    printCarInfo]; – - • - (void) printCarInfo() • – • [[Car alloc] init]; – + • + (void) init { super[init]; return init; } December 2, 2010 S.C. Chen @ VIPLab 26
  15. December 2, 2010 S.C. Chen @ VIPLab 28 NSObject NSArray

    NSString UIResponder UIView UILabel UIControl UISlider UITextFied
  16. • • NSLog() – NSLog(@"Current Make: %@\n", make); – 2010-12-02

    12:05:23.784 HelloWorld[11197:20b] Current Make: VIPLab • CFShow() stderr – CFShow(make); – December 2, 2010 S.C. Chen @ VIPLab 29
  17. • – Car *car = [[Car alloc] init]; – Car

    *car = [[[Car alloc] init] autorelease]; – Car *car = [Car car]; • + (Car *) car { return [[[Car alloc] init] autorelease]; } • December 2, 2010 S.C. Chen @ VIPLab 32
  18. init* December 2, 2010 S.C. Chen @ VIPLab 36 init*

    Car *myCar = [[Car alloc] init];
  19. December 2, 2010 S.C. Chen @ VIPLab 39 @interface Foo

    : NSObject { NSString *m_name; } - (id) initWithName:(NSString *) name; @end
  20. December 2, 2010 S.C. Chen @ VIPLab 40 @implementation Foo

    { - (id) initWithName:(NSString *) name { self = [super init]; if (!self) return nil; m_name = name; [m_name retain]; return self; } - (void) dealloc { [m_name release]; [super dealloc]; } }
  21. December 2, 2010 S.C. Chen @ VIPLab 43 int bar

    (const char *p) { NSString *q = [[NSString alloc] initWithUTF8String: p]; if (SOME_ERROR_OCCURS) { [q release]; return -1; } // ... if (ANOTHER_ERROR_OCCURS) { [q release]; return -1; } [q release]; return 0; } init*
  22. December 2, 2010 S.C. Chen @ VIPLab 44 int bar

    (const char *p) { NSString *q = [NSString stringWithUTF8String:p]; if (SOME_ERROR_OCCURS) { return -1; } // ... if (ANOTHER_ERROR_OCCURS) { return -1; } return 0; }
  23. @property • @property • – @property NSString *make; • –

    @property (retain) NSString *make; • retain – @property (copy) NSString *make; • – @property (readonly) NSString *make; • December 2, 2010 S.C. Chen @ VIPLab 46
  24. @property @interface Person : NSObject { NSString *name; } @property

    (copy) NSString *name; @end @implementation Person @synthesize name; // ... @end December 2, 2010 S.C. Chen @ VIPLab 47
  25. @interface MyDelegate : NSObject { } - (BOOL) applicationShouldTerminateAfterLastWindowClosed: (NSApplication

    *) theApplication; - (BOOL) windowShouldClose:(id) window; @end December 2, 2010 S.C. Chen @ VIPLab 49
  26. @implementation MyDelegate - (BOOL) applicationShouldTerminateAfterLastWindowClosed: (NSApplication *) theApplication: { return

    YES; } - (BOOL) windowShouldClose:(id) window { NSAlert *alert = [[NSAlert alloc] init]; // ... show an alert and get the users’ response if (result == NSAlertFirstButtonReturn) { /* ... */} [alert release]; return NO; } @end December 2, 2010 S.C. Chen @ VIPLab 50
  27. • NSString NSURL NSNumber NSDate NSTimer NSArray NSDictionary NSData •

    – NSArray NSMutableArray • December 2, 2010 S.C. Chen @ VIPLab 53
  28. • – • • – • – • – •

    – December 2, 2010 S.C. Chen @ VIPLab 69
  29. • – UITextView – UILabel – UIImageView – UIWebView –

    MKMapView • MKAnnoatationView MKPinAnnoationView – UIScrollView December 2, 2010 S.C. Chen @ VIPLab 71