//
// Ebook2ViewController.h
// Ebook2
//
// Created by sinnMac05 on 3/25/10.
// Copyright sinn 2010. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "MovieEditorViewController.h"
//@class编译指令是一个提前声明(forward declaration)报告给编译器,找不到Movie这样的类,但不要报错误或警告!
@class Movie;
@interface Ebook2ViewController : UIViewController {
Movie *movie;
//创建一个三个标签,分别永来现实title,boxOfficeGross,summary
UILabel *titleLabel;
UILabel *boxOfficeGrossLabel;
UILabel *summaryLabel;
//添加一个新视图
MovieEditorViewController *editingViewController;
}
//定义属性
@property(nonatomic,retain)Movie *movie;
@property(nonatomic,retain)IBOutlet UILabel *titleLabel;
@property(nonatomic,retain)IBOutlet UILabel *boxOfficeGrossLabel;
@property(nonatomic,retain)IBOutlet UILabel *summaryLabel;
@property(nonatomic,retain)IBOutlet MovieEditorViewController *editingViewController;
-(IBAction)edit;
@end
#import "Ebook2ViewController.h"
#import "Movie.h"
@implementation Ebook2ViewController
@synthesize titleLabel;
@synthesize boxOfficeGrossLabel;
@synthesize summaryLabel;
@synthesize movie;
@synthesize editingViewController;
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
[super viewDidUnload];
Movie *newMovie =[[[Movie alloc] initWithTitle:@"Iron Man"
boxOfficeGross:[NSNumber
numberWithFloat:6500000000.00]
summary:@"amat gun"]autorelease];
self.movie=newMovie;
}
- (void)dealloc {
[super dealloc];
}
-(void) viewWillAppear:(BOOL) animated{
[self viewDidUnload];
[super viewWillAppear:animated];
self.titleLabel.text=self.movie.title;
NSNumberFormatter *formatter=[[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
self.boxOfficeGrossLabel.text=
[formatter stringFromNumber:self.movie.boxOfficeGross];
[formatter release];
self.summaryLabel.text=self.movie.summary;
}
-(IBAction)edit{
self.editingViewController.movie=self.movie;
[self presentModalViewController:self.editingViewController animated:YES];
}
@end
//
// Movie.h
// Ebook2
//
// Created by sinnMac05 on 3/25/10.
// Copyright 2010 sinn. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Movie : NSObject {
//电影名字
NSString *title;
//电影票房
NSNumber *boxOfficeGross;
//电影概述
NSString *summary;
}
//初始化Movie
-(id) initWithTitle:(NSString *) newTitle
boxOfficeGross:(NSNumber *) newBoxOfficeGross
summary:(NSString *) newSummary;
@property(nonatomic,copy)NSString *title;
@property(nonatomic,copy)NSNumber *boxOfficeGross;
@property(nonatomic,copy)NSString *summary;
@end
//
// Movie.m
// Ebook2
//
// Created by sinnMac05 on 3/25/10.
// Copyright 2010 sinn. All rights reserved.
//
#import "Movie.h"
@implementation Movie
@synthesize title;
@synthesize boxOfficeGross;
@synthesize summary;
-(id) initWithTitle:(NSString *)newTitle
boxOfficeGross:(NSNumber *)newBoxOfficeGross
summary:(NSString *)newSummary{
self=[super init];
if(nil!=self){
self.title=newTitle;
self.boxOfficeGross=newBoxOfficeGross;
self.summary=newSummary;
}
return self;
}
-(void) dealloc{
self.title=nil;
self.boxOfficeGross=nil;
self.summary=nil;
[super dealloc];
}
@end
//
// MovieEditorViewController.h
// Ebook2
//
// Created by sinnMac05 on 3/26/10.
// Copyright 2010 sinn. All rights reserved.
//
#import <UIKit/UIKit.h>
@class Movie;
@interface MovieEditorViewController : UIViewController<UITextFieldDelegate> {
IBOutlet UITextField *titleField;
IBOutlet UITextField *boxOfficeGrossField;
IBOutlet UITextField *summaryField;
Movie *movie;
}
-(IBAction)done;
@property (nonatomic,retain) IBOutlet UITextField *titleField;
@property (nonatomic,retain) IBOutlet UITextField *boxOfficeGrossField;
@property (nonatomic,retain) IBOutlet UITextField *summaryField;
@property (nonatomic,retain) Movie *movie;
@end
#import "MovieEditorViewController.h"
#import "Movie.h"
@implementation MovieEditorViewController
@synthesize titleField;
@synthesize boxOfficeGrossField;
@synthesize summaryField;
@synthesize movie;
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//将电影标题给titleField
self.titleField.text=self.movie.title;
//将电影的概述内容给SummayField
self.summaryField.text=self.movie.summary;
//创建一个格式化数字类,NSNumberFormatter,并将boxOfficeStyle(票房率)转化为字符串输出。
NSNumberFormatter *formatter=[[NSNumberFormatter alloc] init];
//设置显示的样式
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
self.boxOfficeGrossField.text=
[formatter stringFromNumber:self.movie.boxOfficeGross];
[formatter release];
}
-(IBAction)done{
//不显示模型视图
[[self parentViewController] dismissModalviewControllerAnimated:YES];
}
-(BOOL)textFieldShouldReturn:(UITextField *) textField{
[textField resignFirstResponder];
return YES;
}
-(void) textFieldDidEndEditing:(UITextField *) textField{
//当文本内容为电影标题时
if(textField==self.titleField){
self.movie.title=self.titleField.text;
//当文本内容为票房率时
}else if(textField==self.boxOfficeGrossField){
//创建格式化数字类NSNumberFormatter
NSNumberFormatter *formatter=[[NSNumberFormatter alloc] init];
//设置输出数字的样式,按原样输出
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
self.movie.boxOfficeGross=
//将所需要格式化的数字,传入NSNumberFormatter进行格式化
[formatter numberFromString:self.boxOfficeGrossField.text];
//释放formatter对象
[formatter release];
}
//当文本内容为电影概述时
else if(textField==self.summaryField){
self.movie.summary=self.summaryField;
}
}
@end