现在的位置: 首页 > 综合 > 正文

IOS成长之路-实现界面切换和数据的传递

2017年10月31日 ⁄ 综合 ⁄ 共 14972字 ⁄ 字号 评论关闭

逻辑过程: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
       
方法,实现把在第二个界面中修改后的值显示在第一个界面中。

 

 

第一个界面:

  1. /*ViewController.h*/  
  2.   
  3. #import <UIKit/UIKit.h>  
  4. @class Movie;  
  5.   
  6. @interface ViewController : UIViewController  
  7. {  
  8.     //创建Movie对象  
  9.     Movie *movie;  
  10.     //定义三个Label  
  11.     UILabel *titleLabel;  
  12.     UILabel *priceLabel;  
  13.     UILabel *summaryLabel;  
  14. }  
  15.   
  16. @property(nonatomic,retain)Movie *movie;  
  17. @property(nonatomic,retain)IBOutlet UILabel *titleLabel;  
  18. @property(nonatomic,retain)IBOutlet UILabel *priceLabel;  
  19. @property(nonatomic,retain)IBOutlet UILabel *summaryLabel;  
  20. //定义切换界面的方法  
  21. -(IBAction)Edit:(id)sender;  
  22. @end  


 

  1. /*ViewController.m*/  
  2.   
  3. #import "ViewController.h"  
  4. #import "Movie.h"  
  5. #import "EditViewController.h"  
  6. @implementation ViewController  
  7. @synthesize movie;  
  8. @synthesize titleLabel;  
  9. @synthesize priceLabel;  
  10. @synthesize summaryLabel;  
  11. - (void)didReceiveMemoryWarning  
  12. {  
  13.     [super didReceiveMemoryWarning];  
  14.     // Release any cached data, images, etc that aren't in use.  
  15. }  
  16.   
  17. #pragma mark - View lifecycle  
  18. //view 被创建的时候初始化,只会调用一次  
  19. - (void)viewDidLoad  
  20. {  
  21.     [super viewDidLoad];  
  22.       
  23.     //初始化创建的Movie对象  给name price summary  
  24.     movie = [[Movie alloc]initWithName:@"Iron man" andPrice:[NSNumber numberWithInt:122] andSummary:@"the movie is good"];  
  25.     //把name添到Label中  
  26.     titleLabel.text = movie.name;  
  27.       
  28.     //price是NSNumber类型,转换成int型   并一字符串的形式添到priceLabel中  
  29.     int tmp = [movie.price intValue];  
  30.     priceLabel.text = [NSString stringWithFormat:@"%d",tmp];  
  31.       
  32.     //把summary添到summaryLabel中  
  33.     summaryLabel.text = movie.summary;  
  34.       
  35.     //上面赋值的过程可以写为  
  36.     //self.movie = movie;  
  37. }  
  38.   
  39. //每次进入view都会调用该方法  实现把修改后的值赋给label  
  40. - (void)viewWillAppear:(BOOL)animated  
  41. {  
  42.     NSLog(@"%@",movie);  
  43.     titleLabel.text = movie.name;  
  44.     int tmp = [movie.price intValue];  
  45.     priceLabel.text = [NSString stringWithFormat:@"%d",tmp];  
  46.     summaryLabel.text = movie.summary;  
  47.       
  48.     [super viewWillAppear:animated];  
  49. }  
  50.   
  51.   
  52. - (void)viewDidUnload  
  53. {  
  54.     [super viewDidUnload];  
  55.     self.titleLabel = nil;  
  56.     self.priceLabel = nil;  
  57.     self.summaryLabel = nil;  
  58.     // Release any retained subviews of the main view.  
  59.     // e.g. self.myOutlet = nil;  
  60. }  
  61.   
  62. - (void)viewDidAppear:(BOOL)animated  
  63. {  
  64.     [super viewDidAppear:animated];  
  65. }  
  66.   
  67. - (void)viewWillDisappear:(BOOL)animated  
  68. {  
  69.     [super viewWillDisappear:animated];  
  70. }  
  71.   
  72. - (void)viewDidDisappear:(BOOL)animated  
  73. {  
  74.     [super viewDidDisappear:animated];  
  75. }  
  76.   
  77. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  78. {  
  79.     // Return YES for supported orientations  
  80.     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);  
  81. }  
  82.   
  83. //实现界面的切换  并把值传递过去  
  84. -(IBAction)Edit:(id)sender  
  85. {  
  86.     //要从此类界面转换到EditViewController类的界面  
  87.     EditViewController *tmpEdit = [[EditViewController alloc]initWithNibName:@"EditViewController" bundle:nil];  
  88.     //- (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated;  
  89.       
  90.     //打印movie对象里面的值  
  91.     NSLog(@"%@",movie);  
  92.     //把movie里面的值(name price summary)赋给 EditViewController 类里面的movie对象editMovie  
  93.     tmpEdit.editMovie = movie;  
  94.       
  95.     //设置翻页效果  
  96.     tmpEdit.modalTransitionStyle = UIModalTransitionStyleCoverVertical;  
  97.     /* 
  98.      其他翻页效果: 
  99.      UIModalTransitionStyleCoverVertical 
  100.      UIModalTransitionStyleFlipHorizontal 
  101.      UIModalTransitionStyleCrossDissolve 
  102.      UIModalTransitionStylePartialCurl 
  103.      */  
  104.       
  105.     [self presentModalViewController:tmpEdit animated:YES];//实现页面的切换  
  106.     [tmpEdit autorelease];  
  107.     NSLog(@"Edit function called");  
  108. }  
  109. @end  


 

界面显示:

 

 

 

第二个界面:

  1. /*EditViewController.h*/  
  2.   
  3. #import <UIKit/UIKit.h>  
  4. @class Movie;  
  5. @interface EditViewController : UIViewController  
  6. {  
  7.     Movie *editMovie;//movie对象  
  8.     //三个textField对象  
  9.     UITextField *textName;  
  10.     UITextField *textPrice;  
  11.     UITextField *textSummary;  
  12. }  
  13. @property(nonatomic,retain)IBOutlet UITextField *textName;  
  14. @property(nonatomic,retain)IBOutlet UITextField *textPrice;  
  15. @property(nonatomic,retain)IBOutlet UITextField *textSummary;  
  16. @property(nonatomic,retain)Movie *editMovie;  
  17. -(IBAction)Back:(id)sender;//定义返回到上一个界面的方法  
  18. @end  


 

  1. /*EditViewController.m*/  
  2.   
  3. #import "EditViewController.h"  
  4. #import "Movie.h"  
  5. @implementation EditViewController  
  6. @synthesize editMovie;  
  7. @synthesize textName;  
  8. @synthesize textPrice;  
  9. @synthesize textSummary;  
  10.   
  11.   
  12. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
  13. {  
  14.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  15.       
  16.     if (self) {  
  17.         // Custom initialization  
  18.     }  
  19.     return self;  
  20. }  
  21.   
  22. - (void)didReceiveMemoryWarning  
  23. {  
  24.     // Releases the view if it doesn't have a superview.  
  25.     [super didReceiveMemoryWarning];  
  26.       
  27.     // Release any cached data, images, etc that aren't in use.  
  28. }  
  29.   
  30. #pragma mark - View lifecycle  
  31.   
  32. /* 
  33. // Implement loadView to create a view hierarchy programmatically, without using a nib. 
  34. - (void)loadView 
  35. { 
  36. } 
  37. */  
  38.   
  39.   
  40. //把从ViewController类中得到的值添加到界面中三个text中  
  41. - (void)viewDidLoad  
  42. {  
  43.     NSLog(@"editMovie = %@",editMovie);  
  44.       
  45.     textName.text = editMovie.name;  
  46.       
  47.     //把NSNumber类型的值转换成int型的值  
  48.     int tmp = [editMovie.price intValue];  
  49.     textPrice.text = [NSString stringWithFormat:@"%d",tmp];  
  50.       
  51.     textSummary.text = editMovie.summary;  
  52.     [super viewDidLoad];  
  53. }  
  54.   
  55.   
  56. - (void)viewDidUnload  
  57. {  
  58.     self.textName = nil;  
  59.     self.textPrice = nil;  
  60.     self.textSummary = nil;  
  61.     [super viewDidUnload];  
  62. }  
  63.   
  64. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  65. {  
  66.     return (interfaceOrientation == UIInterfaceOrientationPortrait);  
  67. }  
  68.   
  69.   
  70. //返回上一个界面  
  71. -(IBAction)Back:(id)sender  
  72. {  
  73.     //- (void)dismissModalViewControllerAnimated:(BOOL)animated;  方法  
  74.     [self dismissModalViewControllerAnimated:YES];  
  75.     NSLog(@"Back function called");  
  76. }  
  77.   
  78.   
  79. -(void)dealloc  
  80. {  
  81.     [textName release];  
  82.     [textPrice release];  
  83.     [textSummary release];  
  84.     [editMovie release];  
  85.     [super dealloc];  
  86. }  
  87.   
  88. //加一个标签  作用是实现把值每修改一次则返回一次  
  89. #pragma mark UITextFiledDelegate methods  
  90. -(BOOL)textFieldShouldReturn:(UITextField *)textField  
  91. {  
  92.     [textField resignFirstResponder];  
  93.     return YES;  
  94. }  
  95. //实现在第二个界面中修改值,然后把修改后的值赋给Movie对象存储起来,在切换到上一个界面时,使用对象输出  
  96. -(void) textFieldDidEndEditing:(UITextField *)textField  
  97. {  
  98.     //判断语句  实现区分name price summary  
  99.     if(textName == textField)  
  100.     {  
  101.         editMovie.name = textField.text;  
  102.     }  
  103.     else if(textPrice == textField)  
  104.     {  
  105.         //转换类型 赋值  
  106.         int tmp = [textField.text intValue];//NSString -(int)intValue  
  107.         editMovie.price = [NSNumber numberWithInt:tmp];  
  108.     }  
  109.     else if(textSummary == textField)  
  110.     {  
  111.         editMovie.summary = textField.text;  
  112.     }  
  113.     //打印  
  114.     NSLog(@"%@",editMovie);  
  115. }  
  116. @end  


 

界面显示:

 

效果图:

 

 

 

 

 

逻辑过程: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
       
方法,实现把在第二个界面中修改后的值显示在第一个界面中。

 

 

第一个界面:

  1. /*ViewController.h*/  
  2.   
  3. #import <UIKit/UIKit.h>  
  4. @class Movie;  
  5.   
  6. @interface ViewController : UIViewController  
  7. {  
  8.     //创建Movie对象  
  9.     Movie *movie;  
  10.     //定义三个Label  
  11.     UILabel *titleLabel;  
  12.     UILabel *priceLabel;  
  13.     UILabel *summaryLabel;  
  14. }  
  15.   
  16. @property(nonatomic,retain)Movie *movie;  
  17. @property(nonatomic,retain)IBOutlet UILabel *titleLabel;  
  18. @property(nonatomic,retain)IBOutlet UILabel *priceLabel;  
  19. @property(nonatomic,retain)IBOutlet UILabel *summaryLabel;  
  20. //定义切换界面的方法  
  21. -(IBAction)Edit:(id)sender;  
  22. @end  


 

  1. /*ViewController.m*/  
  2.   
  3. #import "ViewController.h"  
  4. #import "Movie.h"  
  5. #import "EditViewController.h"  
  6. @implementation ViewController  
  7. @synthesize movie;  
  8. @synthesize titleLabel;  
  9. @synthesize priceLabel;  
  10. @synthesize summaryLabel;  
  11. - (void)didReceiveMemoryWarning  
  12. {  
  13.     [super didReceiveMemoryWarning];  
  14.     // Release any cached data, images, etc that aren't in use.  
  15. }  
  16.   
  17. #pragma mark - View lifecycle  
  18. //view 被创建的时候初始化,只会调用一次  
  19. - (void)viewDidLoad  
  20. {  
  21.     [super viewDidLoad];  
  22.       
  23.     //初始化创建的Movie对象  给name price summary  
  24.     movie = [[Movie alloc]initWithName:@"Iron man" andPrice:[NSNumber numberWithInt:122] andSummary:@"the movie is good"];  
  25.     //把name添到Label中  
  26.     titleLabel.text = movie.name;  
  27.       
  28.     //price是NSNumber类型,转换成int型   并一字符串的形式添到priceLabel中  
  29.     int tmp = [movie.price intValue];  
  30.     priceLabel.text = [NSString stringWithFormat:@"%d",tmp];  
  31.       
  32.     //把summary添到summaryLabel中  
  33.     summaryLabel.text = movie.summary;  
  34.       
  35.     //上面赋值的过程可以写为  
  36.     //self.movie = movie;  
  37. }  
  38.   
  39. //每次进入view都会调用该方法  实现把修改后的值赋给label  
  40. - (void)viewWillAppear:(BOOL)animated  
  41. {  
  42.     NSLog(@"%@",movie);  
  43.     titleLabel.text = movie.name;  
  44.     int tmp = [movie.price intValue];  
  45.     priceLabel.text = [NSString stringWithFormat:@"%d",tmp];  
  46.     summaryLabel.text = movie.summary;  
  47.       
  48.     [super viewWillAppear:animated];  
  49. }  
  50.   
  51.   
  52. - (void)viewDidUnload  
  53. {  
  54.     [super viewDidUnload];  
  55.     self.titleLabel = nil;  
  56.     self.priceLabel = nil;  
  57.     self.summaryLabel = nil;  
  58.     // Release any retained subviews of the main view.  
  59.     // e.g. self.myOutlet = nil;  
  60. }  
  61.   
  62. - (void)viewDidAppear:(BOOL)animated  
  63. {  
  64.     [super viewDidAppear:animated];  
  65. }  
  66.   
  67. - (void)viewWillDisappear:(BOOL)animated  
  68. {  
  69.     [super viewWillDisappear:animated];  
  70. }  
  71.   
  72. - (void)viewDidDisappear:(BOOL)animated  
  73. {  
  74.     [super viewDidDisappear:animated];  
  75. }  
  76.   
  77. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  78. {  
  79.     // Return YES for supported orientations  
  80.     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);  
  81. }  
  82.   
  83. //实现界面的切换  并把值传递过去  
  84. -(IBAction)Edit:(id)sender  
  85. {  
  86.     //要从此类界面转换到EditViewController类的界面  
  87.     EditViewController *tmpEdit = [[EditViewController alloc]initWithNibName:@"EditViewController" bundle:nil];  
  88.     //- (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated;  
  89.       
  90.     //打印movie对象里面的值  
  91.     NSLog(@"%@",movie);  
  92.     //把movie里面的值(name price summary)赋给 EditViewController 类里面的movie对象editMovie  
  93.     tmpEdit.editMovie = movie;  
  94.       
  95.     //设置翻页效果  
  96.     tmpEdit.modalTransitionStyle = UIModalTransitionStyleCoverVertical;  
  97.     /* 
  98.      其他翻页效果: 
  99.      UIModalTransitionStyleCoverVertical 
  100.      UIModalTransitionStyleFlipHorizontal 
  101.      UIModalTransitionStyleCrossDissolve 
  102.      UIModalTransitionStylePartialCurl 
  103.      */  
  104.       
  105.     [self presentModalViewController:tmpEdit animated:YES];//实现页面的切换  
  106.     [tmpEdit autorelease];  
  107.     NSLog(@"Edit function called");  
  108. }  
  109. @end  


 

界面显示:

 

 

 

第二个界面:

  1. /*EditViewController.h*/  
  2.   
  3. #import <UIKit/UIKit.h>  
  4. @class Movie;  
  5. @interface EditViewController : UIViewController  
  6. {  
  7.     Movie *editMovie;//movie对象  
  8.     //三个textField对象  
  9.     UITextField *textName;  
  10.     UITextField *textPrice;  
  11.     UITextField *textSummary;  
  12. }  
  13. @property(nonatomic,retain)IBOutlet UITextField *textName;  
  14. @property(nonatomic,retain)IBOutlet UITextField *textPrice;  
  15. @property(nonatomic,retain)IBOutlet UITextField *textSummary;  
  16. @property(nonatomic,retain)Movie *editMovie;  
  17. -(IBAction)Back:(id)sender;//定义返回到上一个界面的方法  
  18. @end  


 

  1. /*EditViewController.m*/  
  2.   
  3. #import "EditViewController.h"  
  4. #import "Movie.h"  
  5. @implementation EditViewController  
  6. @synthesize editMovie;  
  7. @synthesize textName;  
  8. @synthesize textPrice;  
  9. @synthesize textSummary;  
  10.   
  11.   
  12. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
  13. {  
  14.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  15.       
  16.     if (self) {  
  17.         // Custom initialization  
  18.     }  
  19.     return self;  
  20. }  
  21.   
  22. - (void)didReceiveMemoryWarning  
  23. {  
  24.     // Releases the view if it doesn't have a superview.  
  25.     [super didReceiveMemoryWarning];  
  26.       
  27.     // Release any cached data, images, etc that aren't in use.  
  28. }  
  29.   
  30. #pragma mark - View lifecycle  
  31.   
  32. /* 
  33. // Implement loadView to create a view hierarchy programmatically, without using a nib. 
  34. - (void)loadView 
  35. { 
  36. } 
  37. */  
  38.   
  39.   
  40. //把从ViewController类中得到的值添加到界面中三个text中  
  41. - (void)viewDidLoad  
  42. {  
  43.     NSLog(@"editMovie = %@",editMovie);  
  44.       
  45.     textName.text = editMovie.name;  
  46.       
  47.     //把NSNumber类型的值转换成int型的值  
  48.     int tmp = [editMovie.price intValue];  
  49.     textPrice.text = [NSString stringWithFormat:@"%d",tmp];  
  50.       
  51.     textSummary.text = editMovie.summary;  
  52.     [super viewDidLoad];  
  53. }  
  54.   
  55.   
  56. - (void)viewDidUnload  
  57. {  
  58.     self.textName = nil;  
  59.     self.textPrice = nil;  
  60.     self.textSummary = nil;  
  61.     [super viewDidUnload];  
  62. }  
  63.   
  64. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  65. {  
  66.     return (interfaceOrientation == UIInterfaceOrientationPortrait);  
  67. }  
  68.   
  69.   
  70. //返回上一个界面  
  71. -(IBAction)Back:(id)sender  
  72. {  
  73.     //- (void)dismissModalViewControllerAnimated:(BOOL)animated;  方法  
  74.     [self dismissModalViewControllerAnimated:YES];  
  75.     NSLog(@"Back function called");  
  76. }  
  77.   
  78.   
  79. -(void)dealloc  
  80. {  
  81.     [textName release];  
  82.     [textPrice release];  
  83.     [textSummary release];  
  84.     [editMovie release];  
  85.     [super dealloc];  
  86. }  
  87.   
  88. //加一个标签  作用是实现把值每修改一次则返回一次  
  89. #pragma mark UITextFiledDelegate methods  
  90. -(BOOL)textFieldShouldReturn:(UITextField *)textField  
  91. {  
  92.     [textField resignFirstResponder];  
  93.     return YES;  
  94. }  
  95. //实现在第二个界面中修改值,然后把修改后的值赋给Movie对象存储起来,在切换到上一个界面时,使用对象输出  
  96. -(void) textFieldDidEndEditing:(UITextField *)textField  
  97. {  
  98.     //判断语句  实现区分name price summary  
  99.     if(textName == textField)  
  100.     {  
  101.         editMovie.name = textField.text;  
  102.     }  
  103.     else if(textPrice == textField)  
  104.     {  
  105.         //转换类型 赋值  
  106.         int tmp = [textField.text intValue];//NSString -(int)intValue  
  107.         editMovie.price = [NSNumber numberWithInt:tmp];  
  108.     }  
  109.     else if(textSummary == textField)  
  110.     {  
  111.         editMovie.summary = textField.text;  
  112.     }  
  113.     //打印  
  114.     NSLog(@"%@",editMovie);  
  115. }  
  116. @end  


 

界面显示:

 

效果图:

 

 

 

 

 

抱歉!评论已关闭.