逻辑过程:ViewController.xib为第一个界面,里面显示的是Movie对象存储的name,price,summary 三个属性,会通过
- (void)viewDidLoad 这个方法给三个属性赋值并在界面中显示出来,当点击界面的Edit按钮的时候,会调用ViewController类中的
-(IBAction)Edit:(id)sender; 方法切换到EditViewController.xib界面,而且还会把第一个界面中的 name,price,summary 的值传递到这个界面中,
调用EditViewController这个类中的 - (void)viewDidLoad 这个方法为这个类中的Movie对象赋值,并在TextField控件中显示出来,通过这个类中的
-(BOOL)textFieldShouldReturn:(UITextField *)textField;
-(void) textFieldDidEndEditing:(UITextField *)textField;
这两个方法实现对三个属性值的修改并把修改后的值重新付给了Movie对象,然后点击Back按钮调用
-(IBAction)Back:(id)sender 方法返回上一个界面,然后还会通过当前界面(也就是第一个界面)的类中的
- (void)viewWillAppear:(BOOL)animated 方法,实现把在第二个界面中修改后的值显示在第一个界面中。
/*ViewController.h*/ #import <UIKit/UIKit.h> @class Movie; @interface ViewController : UIViewController { //创建Movie对象 Movie *movie; //定义三个Label UILabel *titleLabel; UILabel *priceLabel; UILabel *summaryLabel; } @property(nonatomic,retain)Movie *movie; @property(nonatomic,retain)IBOutlet UILabel *titleLabel; @property(nonatomic,retain)IBOutlet UILabel *priceLabel; @property(nonatomic,retain)IBOutlet UILabel *summaryLabel; //定义切换界面的方法 -(IBAction)Edit:(id)sender; @end
/*ViewController.m*/ #import "ViewController.h" #import "Movie.h" #import "EditViewController.h" @implementation ViewController @synthesize movie; @synthesize titleLabel; @synthesize priceLabel; @synthesize summaryLabel; - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } #pragma mark - View lifecycle //view 被创建的时候初始化,只会调用一次 - (void)viewDidLoad { [super viewDidLoad]; //初始化创建的Movie对象 给name price summary movie = [[Movie alloc]initWithName:@"Iron man" andPrice:[NSNumber numberWithInt:122] andSummary:@"the movie is good"]; //把name添到Label中 titleLabel.text = movie.name; //price是NSNumber类型,转换成int型 并一字符串的形式添到priceLabel中 int tmp = [movie.price intValue]; priceLabel.text = [NSString stringWithFormat:@"%d",tmp]; //把summary添到summaryLabel中 summaryLabel.text = movie.summary; //上面赋值的过程可以写为 //self.movie = movie; } //每次进入view都会调用该方法 实现把修改后的值赋给label - (void)viewWillAppear:(BOOL)animated { NSLog(@"%@",movie); titleLabel.text = movie.name; int tmp = [movie.price intValue]; priceLabel.text = [NSString stringWithFormat:@"%d",tmp]; summaryLabel.text = movie.summary; [super viewWillAppear:animated]; } - (void)viewDidUnload { [super viewDidUnload]; self.titleLabel = nil; self.priceLabel = nil; self.summaryLabel = nil; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } //实现界面的切换 并把值传递过去 -(IBAction)Edit:(id)sender { //要从此类界面转换到EditViewController类的界面 EditViewController *tmpEdit = [[EditViewController alloc]initWithNibName:@"EditViewController" bundle:nil]; //- (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated; //打印movie对象里面的值 NSLog(@"%@",movie); //把movie里面的值(name price summary)赋给 EditViewController 类里面的movie对象editMovie tmpEdit.editMovie = movie; //设置翻页效果 tmpEdit.modalTransitionStyle = UIModalTransitionStyleCoverVertical; /* 其他翻页效果: UIModalTransitionStyleCoverVertical UIModalTransitionStyleFlipHorizontal UIModalTransitionStyleCrossDissolve UIModalTransitionStylePartialCurl */ [self presentModalViewController:tmpEdit animated:YES];//实现页面的切换 [tmpEdit autorelease]; NSLog(@"Edit function called"); } @end
/*EditViewController.h*/ #import <UIKit/UIKit.h> @class Movie; @interface EditViewController : UIViewController { Movie *editMovie;//movie对象 //三个textField对象 UITextField *textName; UITextField *textPrice; UITextField *textSummary; } @property(nonatomic,retain)IBOutlet UITextField *textName; @property(nonatomic,retain)IBOutlet UITextField *textPrice; @property(nonatomic,retain)IBOutlet UITextField *textSummary; @property(nonatomic,retain)Movie *editMovie; -(IBAction)Back:(id)sender;//定义返回到上一个界面的方法 @end
/*EditViewController.m*/ #import "EditViewController.h" #import "Movie.h" @implementation EditViewController @synthesize editMovie; @synthesize textName; @synthesize textPrice; @synthesize textSummary; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (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. } #pragma mark - View lifecycle /* // Implement loadView to create a view hierarchy programmatically, without using a nib. - (void)loadView { } */ //把从ViewController类中得到的值添加到界面中三个text中 - (void)viewDidLoad { NSLog(@"editMovie = %@",editMovie); textName.text = editMovie.name; //把NSNumber类型的值转换成int型的值 int tmp = [editMovie.price intValue]; textPrice.text = [NSString stringWithFormat:@"%d",tmp]; textSummary.text = editMovie.summary; [super viewDidLoad]; } - (void)viewDidUnload { self.textName = nil; self.textPrice = nil; self.textSummary = nil; [super viewDidUnload]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } //返回上一个界面 -(IBAction)Back:(id)sender { //- (void)dismissModalViewControllerAnimated:(BOOL)animated; 方法 [self dismissModalViewControllerAnimated:YES]; NSLog(@"Back function called"); } -(void)dealloc { [textName release]; [textPrice release]; [textSummary release]; [editMovie release]; [super dealloc]; } //加一个标签 作用是实现把值每修改一次则返回一次 #pragma mark UITextFiledDelegate methods -(BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; } //实现在第二个界面中修改值,然后把修改后的值赋给Movie对象存储起来,在切换到上一个界面时,使用对象输出 -(void) textFieldDidEndEditing:(UITextField *)textField { //判断语句 实现区分name price summary if(textName == textField) { editMovie.name = textField.text; } else if(textPrice == textField) { //转换类型 赋值 int tmp = [textField.text intValue];//NSString -(int)intValue editMovie.price = [NSNumber numberWithInt:tmp]; } else if(textSummary == textField) { editMovie.summary = textField.text; } //打印 NSLog(@"%@",editMovie); } @end