아이폰프로그래밍(2)

1,011 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,011
On SlideShare
0
From Embeds
0
Number of Embeds
322
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

아이폰프로그래밍(2)

  1. 1. (2) Bit Academy
  2. 2. • MVC • • • • NSDictionary
  3. 3. MVC • Model View Controller : , , . • Model : • View : • Controller :
  4. 4. MVC LightTheCandle • LightTheCandleAppDelegate • MVC Candle class Controller
  5. 5. NSDictionary
  6. 6. Candle
  7. 7. Candle • LightTheCandleAppDelegate candleState, candleOffImage, candleOnImage Candle . • LightTheCandleAppDelegate Candle .
  8. 8. • candle candleState . • candleState getter • candleState setter set + CandleState = setCandleState.
  9. 9. Candle.h #import <Foundation/Foundation.h> @interface Candle : NSObject { ! BOOL candleState; ! UIImage *candleOnImage; ! UIImage *candleOffImage; } - (BOOL)candleState; - (void)setCandleState:(BOOL)newState; - (UIImage *)candleOnImage; - (void)setCandleOnImage:(UIImage *)newImage; - (UIImage *)candleOffImage; - (void)setCandleOffImage:(UIImage *)newImage; /* @property (assign) BOOL candleState; @property (retain) UIImage *candleOnImage; @property (retain) UIImage *candleOffImage; */ @end
  10. 10. • LightTheCandleAppDelegate applicationDidFinishLaunching: Candle . • -(id)init . NSObject
  11. 11. Candle.m init init ! self super init ; ! self ! ! candleState ; ! ! candleOnPath mainBundle pathForResource ofType ; ! ! candleOffPath mainBundle pathForResource ofType ; ! ! candleOffImage UIImage alloc initWithContentsOfFile candleOffPath ; ! ! candleOnImage UIImage alloc initWithContentsOfFile candleOnPath ; !   ! self;
  12. 12. Candle candleState ! candleState; Candle.m setCandleState newState ! candleState newState;   UIImage candleOffImage ! candleOffImage; setCandleOffImage UIImage newImage ! UIImage oldImage candleOffImage; ! oldImage newImage ! ! candleOffImage newImage retain ; ! ! oldImage oldImage release ; !   UIImage candleOnImage ! candleOnImage; setCandleOnImage UIImage newImage ! UIImage oldImage candleOnImage; ! oldImage newImage ! ! candleOnImage newImage retain ; ! ! oldImage oldImage release ; !
  13. 13. LightTheCandleAppDelegate • Candle • Candle *myCandle .h • myCandle = [[Candle alloc] init] .m • @class Candle .m #import “Candle.h”
  14. 14. applicationDidFinishLaunching applicationDidFinishLaunching UIApplication application   ! myCandle Candle alloc init ; !   ! candleImageView setImage myCandle candleOffImage ; onOffSwitch.on = [myCandle candleState]; candleStateLabel.text = @”Candle is Off. Please light on”; window makeKeyAndVisible ;
  15. 15. togggleCandle IBAction toggleCandle sender ! myCandle setCandleState myCandle candleState ; ! myCandle candleState ! ! ! ! candleImageView setImage myCandle candleOnImage ; ! ! onOffSwitch.on ; ! ! candleStateLabel.text ; ! ! ! ! ! candleImageView setImage myCandle candleOffImage ; ! ! onOffSwitch.on ; ! ! candleStateLabel.text ; !
  16. 16. • . • • Property synthesize
  17. 17. property Candle Candle Candle ! candleState; ! candleState; ! UIImage candleOffImage; ! UIImage candleOffImage; ! UIImage candleOnImage; ! UIImage candleOnImage;     candleState; assign candleState; setCandleState newState; retain UIImage candleOffImage;   retain UIImage candleOnImage; UIImage candleOffImage;   setCandleOffImage UIImage newImage;     UIImage candleOnImage; setCandleOnImage UIImage newImage;   Candle   candleState ! candleState; setCandleState newState ! candleState newState; UIImage candleOffImage ! candleOffImage; setCandleOffImage UIImage newImage Candle ! UIImage oldImage candleOffImage; ! oldImage newImage   ! ! candleOffImage newImage retain ; candleState; ! ! oldImage oldImage release ; candleOffImage; ! candleOnImage; UIImage candleOnImage ! candleOnImage; setCandleOnImage UIImage newImage ! UIImage oldImage candleOnImage; ! oldImage newImage ! ! candleOnImage newImage retain ; ! ! oldImage oldImage release ; !
  18. 18. NSDictionary • Candle *myCandle -> .h file NSMutableDictionary *myCandleDict • myCandle = [[Candle alloc] init] -> myCandleDict = [[NSMutableDictionary alloc] initWithCapacity:10] appDelegate.m file
  19. 19. -applicationDidFinishLaunching candleState = NO; NSString *candleOnPath = [[NSBundle mainBundle] pathForResource: @”candle on” ofType: @”jpg”]; NSString *candleOffPath = [[NSBundle mainBundle] pathForResource: @”candle off” ofType: @”jpg”]; candleOffImage = [[UIImage alloc] initWithContentsOfFile:candleOffPath]; candleOnImage = [[UIImage alloc] initWithContentsOfFile:candleOnPath]; [myCandleDict setValue: [NSNumber numberWithBool: candleState] forKey:@”candleState”]; [myCandleDict setValue: candleOffImage forKey:@”candleOffImage”]; [myCandleDict setValue:candleOnImage forKey:@”candleOnImage”]; Integer, Float, Bool NSNumber
  20. 20. toggleCandle -(IBAction)toggleCandle:(id)sender { BOOL candleState = [[myCandleDict valueForKey: @”candleState”] boolValue]; [myCandleDict setValue:[NSNumber numberWithBool:!candleState] forKey:@”candleState”]; if(!candleState) { //On [candleImageView setImage:[myCandleDict valueForKey:@”candleImage”]]; onOffSwitch.on = YES; candleStateLabel.text = @”Candle is now on”; } else { //Off [candleImageView setImage: [myCandleDict valueForKey: @”candleOffImage”]]; onOffSwitch.on = NO; candleStateLabel.text = @”Candle is off. please light on”; } }

×