ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
(2)
               Bit Academy
• MVC
               •
               •
               •
               • NSDictionary
MVC
               •   Model View Controller :   ,
                                ,
                                        .


               •   Model :

               •   View :

               •   Controller :
MVC
                LightTheCandle



               • LightTheCandleAppDelegate
               • MVC                   Candle class
                 Controller
NSDictionary
Candle
Candle


               • LightTheCandleAppDelegate
                            candleState, candleOffImage,
                 candleOnImage Candle
                        .

               • LightTheCandleAppDelegate       Candle

                                    .
• candle                     candleState

                          .

               • candleState                     getter


               • candleState                     setter
                 set + CandleState = setCandleState.
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
• LightTheCandleAppDelegate
                 applicationDidFinishLaunching:
                                       Candle
                                                  .

               • -(id)init                            .
                 NSObject
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;
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 ;
               !
LightTheCandleAppDelegate



               • Candle
               • Candle *myCandle .h
               • myCandle = [[Candle alloc] init] .m
               • @class Candle .m            #import
                 “Candle.hâ€
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 ;
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             	  	    	    	    	    ;
               !
•
                                         .

               •
               • Property   synthesize
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 ;
                    !
NSDictionary


               •   Candle *myCandle ->     .h ï¬le
                   NSMutableDictionary *myCandleDict
               • myCandle = [[Candle alloc] init] ->
                   myCandleDict = [[NSMutableDictionary
                   alloc] initWithCapacity:10]
                                        appDelegate.m ï¬le
-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
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â€;
         }
     }

More Related Content

ì•Ñ«´í°í”„로그래ë°(2)

  • 1. (2) Bit Academy
  • 2. • MVC • • • • NSDictionary
  • 3. MVC • Model View Controller : , , . • Model : • View : • Controller :
  • 4. MVC LightTheCandle • LightTheCandleAppDelegate • MVC Candle class Controller
  • 7. Candle • LightTheCandleAppDelegate candleState, candleOffImage, candleOnImage Candle . • LightTheCandleAppDelegate Candle .
  • 8. • candle candleState . • candleState getter • candleState setter set + CandleState = setCandleState.
  • 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. • LightTheCandleAppDelegate applicationDidFinishLaunching: Candle . • -(id)init . NSObject
  • 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. 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. LightTheCandleAppDelegate • Candle • Candle *myCandle .h • myCandle = [[Candle alloc] init] .m • @class Candle .m #import “Candle.hâ€
  • 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. 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. • . • • Property synthesize
  • 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. NSDictionary • Candle *myCandle -> .h ï¬le NSMutableDictionary *myCandleDict • myCandle = [[Candle alloc] init] -> myCandleDict = [[NSMutableDictionary alloc] initWithCapacity:10] appDelegate.m ï¬le
  • 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. 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â€; } }