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 省略









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值