IOS成長之路-實現介面切換和資料的傳遞

來源:互聯網
上載者:User

邏輯過程:ViewController.xib為第一個介面,裡面顯示的是MovieObject Storage Service的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;}//實現在第二個介面中修改值,然後把修改後的值賦給MovieObject Storage Service起來,在切換到上一個介面時,使用對象輸出-(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
 介面顯示:

 :

 

 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.