아이폰프로그래밍(2)
- 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
- 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 file
NSMutableDictionary *myCandleDict
• myCandle = [[Candle alloc] init] ->
myCandleDict = [[NSMutableDictionary
alloc] initWithCapacity:10]
appDelegate.m file
- 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”;
}
}