iphone-實現頁面的切換和修改

//

//  movie.h

//  day04

//

// 

//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.

//


#import <Foundation/Foundation.h>


@interface movie : NSObject <NSCopying, NSCoding>


{

    NSString* name;

    int price;

    NSString* summary;

    

}


@property(nonatomic,copy) NSString* name;


@property(nonatomic,assign)int price;

@property(nonatomic,copy)NSString* summary;



-(id)init;

-(id)initWithName:(NSString*)_name andPrice:(int)_price andSummary:(NSString*)_summary;


-(void)encodeWithCoder:(NSCoder *)aCoder;

-(id)initWithCoder:(NSCoder *)aDecoder;


-(NSComparisonResult)compareName:(id)element;

-(NSComparisonResult)comparePrice:(id)element;

-(NSComparisonResult)compareSummary:(id)element;



@end




//

//  movie.m

//  day04

//

//

//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.

//


#import "movie.h"


@implementation movie


@synthesize  name;

@synthesize  price;

@synthesize  summary;



-(id)init{

    

    self=[super init];

    

    if(!self){

        

        return nil;

    }

    

    name=[[NSString alloc] initWithFormat:@"zhang"];

    if(!name){

        [self release];

        return nil;

    }

    

   // [name autorelease];

    

    price=15;

    

    summary=[[NSString alloc] initWithFormat:@"asdf"];

    if(!summary){

        [self release];

        return nil;

    }

    

  // [summary autorelease];

    

    return self;

}



-(id)initWithName:(NSString*)_name andPrice:(int)_price andSummary:(NSString*)_summary{


    if(!_name||_price<0||!_summary){

        NSLog(@"--------------");

        [self release];    

        return nil;

    

    }

    

    self=[super init];//這個地方應該是調用誰的初始化方法

    

    if(!self){

    

        return nil;

    }


    

    name=[[NSString alloc] initWithFormat:@"%@",_name];

    if(!name){

        [self release];

        return nil;

    }

    

    //[name autorelease];

    

    price=_price;

    

    

    

    summary=[[NSString alloc] initWithFormat:@"%@",_summary];

    if(!summary){

        [self release];

        return nil;

    }

    

   //[summary autorelease];

    

    return self;


    

}


-(NSString*)description{

    

    NSString* ns= [[NSString alloc]initWithFormat:@"name%@,price%d,summary%@",name,price,summary];

    [ns autorelease];

    return ns;

}


-(void)dealloc{

    

  

      [name release];

      [summary release];


      [super dealloc];  

}


-(id)copyWithZone:(NSZone *)zone{


    movie* m=[[movie allocWithZone:zone]initWithName:@"adsfadf" andPrice:1 andSummary:@"qer"];


    return m;

}


-(void)encodeWithCoder:(NSCoder *)aCoder{

    [aCoder encodeObject:name forKey:@"name"]; 

    [aCoder encodeInt:price forKey:@"price"];

    [aCoder encodeObject:summary forKey:@"summary"];


}


-(id)initWithCoder:(NSCoder *)aDecoder{


    if((self=[super init])){

        

        self.name=[aDecoder decodeObjectForKey:@"name"];

        self.price=[aDecoder decodeIntForKey:@"price"];

        self.summary=[aDecoder decodeObjectForKey:@"summary"];

    

    }

    

    return self;


}



-(NSComparisonResult)compareName:(id)element

{

//NSString* str1 = [NSString stringWithString:name];

//NSString* str2 = [NSString stringWithString:[element name]];

//return [str1 compare:str2];

    return [name compare:[element name]];

}


-(NSComparisonResult)comparePrice:(id)element{


    NSNumber* n1=[NSNumber numberWithInt:price];

    NSNumber* n2=[NSNumber numberWithInt:[element price]];

    

    return [n1 compare:n2];

}


-(NSComparisonResult)compareSummary:(id)element{

    

    return [summary compare:[element summary]];

    

}




@end




//

//  AppDelegate.h

//  Iphome_day01

//

//  

//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.

//


#import <UIKit/UIKit.h>


@class ViewController;



@interface AppDelegate : UIResponder <UIApplicationDelegate>


@property (strong,nonatomic)UINavigationController* navagate;

@property (strong, nonatomic) UIWindow *window;


@property (strong, nonatomic) ViewController *viewController;



@end




//

//  AppDelegate.m

//  Iphome_day01

//

//  

//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.

//


#import "AppDelegate.h"


#import "ViewController.h"


@implementation AppDelegate


@synthesize window = _window;

@synthesize viewController = _viewController;

@synthesize navagate=_navagate;


- (void)dealloc

{

    [_window release];

    [_viewController release];

    [super dealloc];

}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    // Override point for customization after application launch.

    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];

    

    _navagate = [[UINavigationController alloc]initWithRootViewController:self.viewController];

    

    [self.window addSubview:_navagate.view];

    

    self.window.rootViewController = self.viewController;

    

    [self.window makeKeyAndVisible];

    return YES;

}


- (void)applicationWillResignActive:(UIApplication *)application

{

    /*

     Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

     Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

     */

}


- (void)applicationDidEnterBackground:(UIApplication *)application

{

    /*

     Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 

     If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

     */

}


- (void)applicationWillEnterForeground:(UIApplication *)application

{

    /*

     Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

     */

}


- (void)applicationDidBecomeActive:(UIApplication *)application

{

    /*

     Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

     */

}


- (void)applicationWillTerminate:(UIApplication *)application

{

    /*

     Called when the application is about to terminate.

     Save data if appropriate.

     See also applicationDidEnterBackground:.

     */

}


@end





//

//  ViewController.h

//  Iphome_day01

//

//  

//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.

//


#import <UIKit/UIKit.h>


@class movie;


@interface ViewController : UIViewController


{


    movie * mo;

    UILabel* label1;

    UILabel* label2;

    UILabel* label3;


}


@property(nonatomic,retain)movie *mo;


@property(nonatomic,retain) IBOutlet UILabel* label1;

@property(nonatomic,retain) IBOutlet UILabel* label2;

@property(nonatomic,retain) IBOutlet UILabel* label3;


-(IBAction)Edit:(id)sender;//實現



//系統會每次都調用該方法

- (void)viewWillAppear:(BOOL)animated;

@end




//

//  ViewController.m

//  Iphome_day01

//

//  

//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.

//


#import "ViewController.h"

#import "movie.h"

#import "EditViewController.h"


@implementation ViewController

@synthesize mo;

@synthesize label1;

@synthesize label2;

@synthesize label3;



- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.

}


#pragma mark - View lifecycle


- (void)viewDidLoad

{

    mo=[[movie alloc]initWithName:@"龍門飛甲" andPrice:200 andSummary:@"古裝片"];

    

    self.title=@"電影詳情";

    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

}


- (void)viewDidUnload

{

    label1=nil;

    label2=nil;

    label3=nil;

    mo=nil;

    [super viewDidUnload];

    // Release any retained subviews of the main view.

    // e.g. self.myOutlet = nil;

}


- (void)viewWillAppear:(BOOL)animated

{

    

    label1.text= mo.name;

    label2.text=[NSString stringWithFormat:@"%d",mo.price];

    label3.text=mo.summary;


    

    

    [super viewWillAppear:animated];

}


- (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);

}



-(void)dealloc{


    label1=nil;

    label2=nil;

    label3=nil;

    

    [mo release];

    [super dealloc];

}


-(IBAction)Edit:(id)sender{


    EditViewController *edit=[[EditViewController alloc]initWithNibName:@"EditViewController" bundle:nil];

    

        edit.editMovie=mo;

     

   /*

    UIModalTransitionStyleCoverVertical = 0,

    UIModalTransitionStyleFlipHorizontal,//翻轉

    UIModalTransitionStyleCrossDissolve,//漸變

    #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2

    UIModalTransitionStylePartialCurl,//翻頁

    

    */

    

    edit.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;

    

    

  //  [self presentModalViewController:edit animated:YES ];//UIViewController 的壓棧方法

    

    [self.navigationController pushViewController:edit animated:YES];//導航UINavigationController的壓棧方法

    

    [edit autorelease];

    

    

    NSLog(@"edit called!!!");


}




@end



viewcontroller.xib 省略



//

//  EditViewController.h

//  Iphome_day01

//

// 

//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.

//


#import <UIKit/UIKit.h>

#import "movie.h"

@class ViewController;


@interface EditViewController : UIViewController <UITextFieldDelegate>


{

    movie* editMovie;

    UITextField *NameField;

    UITextField *PriceField;

    UITextField *SummaryField;

    

}


@property(nonatomic,retain) IBOutlet UITextField* NameField;

@property(nonatomic,retain) IBOutlet UITextField* PriceField;

@property(nonatomic,retain) IBOutlet UITextField* SummaryField;

@property(nonatomic,retain)movie* editMovie;


-(BOOL)textFieldShouldReturn:(UITextField *)textField;//去除鍵盤

-(IBAction)Back:(id)sender;


@end




//

//  EditViewController.m

//  Iphome_day01

//

//

//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.

//


#import "EditViewController.h"

#import "ViewController.h"

#import "movie.h"

@implementation EditViewController


@synthesize NameField;

@synthesize PriceField;

@synthesize SummaryField;

@synthesize editMovie;


- (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


- (void)viewDidLoad

{

    NSLog(@"%@",editMovie);

    

    NameField.text=editMovie.name;

    PriceField.text=[NSString stringWithFormat:@"%d", editMovie.price];

    SummaryField.text=editMovie.summary;

    

    [super viewDidLoad];

    // Do any additional setup after loading the view from its nib.

}


- (void)viewDidUnload

{

    NameField=nil;

    PriceField=nil;

    SummaryField=nil;

    

    [super viewDidUnload];

    // Release any retained subviews of the main view.

    // e.g. self.myOutlet = nil;

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    // Return YES for supported orientations

    return (interfaceOrientation == UIInterfaceOrientationPortrait);

}


-(IBAction)Back:(id)sender{


   // [self dismissModalViewControllerAnimated:YES];

    

    [self.navigationController popToRootViewControllerAnimated:YES];

    

    NSLog(@"back called!!");

}



-(void)dealloc{


    [NameField release];

    [PriceField release];

    [SummaryField release];

    [super dealloc];

}


//去掉鍵盤

-(BOOL)textFieldShouldReturn:(UITextField *)textField{


    [textField resignFirstResponder];

    return YES;


}


-(void)textFieldDidEndEditing:(UITextField *)textField{


    if(NameField==textField){

    

        //textfield的文本給name賦值

        editMovie.name=textField.text;

    }

    else if(PriceField==textField){

    

        editMovie.price=[textField.text intValue];

        

        

    }else{

    

        editMovie.summary=textField.text;

    

    }

    

    NSLog(@"%@",editMovie);

    

}




@end




EditViewController.xib 省略









發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章