iPhone界面跳转及页面传值
2015-09-16 10:01
537 查看
由于第一次博客,可能有很多差强人意的地方,但还是试着写一下吧
这次的内容如标题一样,那么以下内容为要实现的功能。
1,创建Empty Application的模板工程,添加model类Movie,包含三个属性: NSString* title;Number* boxOfficeGross;NSString* summary;并添加相应的 初始化方法,set,get方法,以及NSCoding,NSCoying等协议
2,在根视图控制器中(MovieViewController)声明一个Movie对象,并将此对象的内容显示在界面中(label作为显示控件)。
3,点击在根视图控制器中的“修改”按钮,进入EditViewController,并将 根视图的label内容显示在EditViewController的三个textField中。
4,修改DetailViewController的textField中的值并返回到根视图时,label的值相 应改变。
这次的内容如标题一样,那么以下内容为要实现的功能。
1,创建Empty Application的模板工程,添加model类Movie,包含三个属性: NSString* title;Number* boxOfficeGross;NSString* summary;并添加相应的 初始化方法,set,get方法,以及NSCoding,NSCoying等协议
#import <Foundation/Foundation.h> @interface Movie : NSObject<NSCopying,NSCoding> @property(nonatomic,strong)NSString* title; @property(nonatomic,strong)NSNumber* boxOfficeGross; @property(nonatomic,strong)NSString* summary; -(id)initWithTitle:(NSString*)title andNum:(NSNumber*)boxOfficeGross andSum:(NSString*)summary; +(id)MovieWithTitle:(NSString*)title andNum:(NSNumber*)boxOfficeGross andSum:(NSString*)summary; @end
#import "Movie.h" @implementation Movie -(id)initWithTitle:(NSString*)title andNum:(NSNumber*)boxOfficeGross andSum:(NSString*)summary { if (self = [super init]) { self.title = title; self.boxOfficeGross = boxOfficeGross; self.summary = summary; } return self; } +(id)MovieWithTitle:(NSString*)title andNum:(NSNumber*)boxOfficeGross andSum:(NSString*)summary { Movie * movie = [[Movie alloc] initWithTitle:title andNum:boxOfficeGross andSum:summary]; return movie; } -(void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeObject:self.title forKey:@"TITLE"]; [aCoder encodeObject:self.boxOfficeGross forKey:@"BOXGROSS"]; [aCoder encodeObject:self.summary forKey:@"SUMMARY"]; } -(id)initWithCoder:(NSCoder *)aDecoder { if (self = [super init]) { self.title = [aDecoder decodeObjectForKey:@"TITLE"]; self.boxOfficeGross = [aDecoder decodeObjectForKey:@"BOXGROSS"]; self.summary = [aDecoder decodeObjectForKey:@"SUMMARY"]; } return self; } -(id)copyWithZone:(NSZone *)zone { Movie * movie = [[Movie allocWithZone:zone] init]; [movie setTitle:self.title]; [movie setBoxOfficeGross:self.boxOfficeGross]; [movie setSummary:self.summary]; return movie; } -(NSString *)description { return [NSString stringWithFormat:@"%@%@%@",self.title,self.boxOfficeGross,self.summary]; } - (void)dealloc { } @end
2,在根视图控制器中(MovieViewController)声明一个Movie对象,并将此对象的内容显示在界面中(label作为显示控件)。
#import <UIKit/UIKit.h> #import "Movie.h" @interface MainViewController : UIViewController @property(nonatomic,retain)Movie* movie; @end #import "MainViewController.h" #import "Movie.h" #import "EditeViewController.h" @interface MainViewController () @property(nonatomic,strong)UILabel*label11; @property(nonatomic,strong)UILabel*label22; @property(nonatomic,strong)UILabel*label33; @end @implementation MainViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { } return self; } - (void)viewDidLoad { [super viewDidLoad]; [self addclass]; [self _initUI]; } // 刷新:当第二界面重新跳转到第一页面时,用于刷新第一页面的显示内容 - (void)updateUI { _label11.text = _movie.title; _label22.text = [NSString stringWithFormat:@"%@",_movie.boxOfficeGross]; _label33.text = _movie.summary; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self updateUI]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (void)click:(id)sender {
<span style="font-family: Arial, Helvetica, sans-serif;">//<span style="white-space:pre"> </span>点击事件,跳转到第二页面,同时将第一页面中label的内容传给第二页面的指定控件接收</span>
EditeViewController * viewController = [[EditeViewController alloc] init]; viewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:viewController animated:YES completion:nil];
<span style="font-family: Arial, Helvetica, sans-serif;">//<span style="white-space:pre"> </span>传值方法之:控件传值(需要写在跳转方法的后面,如果是实例变量传值,那么就要写在跳转方法的前面)</span>
viewController.text1.text = _label11.text; viewController.text2.text = _label22.text; viewController.text3.text = _label33.text;
<span style="font-family: Arial, Helvetica, sans-serif;">//<span style="white-space:pre"> </span>利用retain浅拷贝实现第二页面内容与第一页面内容同步。(指向同一空间)</span>
viewController.movie2 = self.movie; } - (void)_initUI {
<span style="font-family: Arial, Helvetica, sans-serif;">//<span style="white-space:pre"> </span>初始化一个背景视图</span>
UIImageView* image = [[UIImageView alloc] init]; image.frame = CGRectMake(0, 0, 320, 480); [image setImage:[UIImage imageNamed:@"路.jpg"]]; [self.view addSubview:image]; UIImageView* image1 = [[UIImageView alloc] init]; image1.frame = CGRectMake(150, 100, 140, 40); // [image1 setImage:[UIImage imageNamed:@"next_btn@2x"]]; [self.view addSubview:image1]; UIImageView* image2 = [[UIImageView alloc] init]; image2.frame = CGRectMake(150, 200, 140, 40); // [image2 setImage:[UIImage imageNamed:@"next_btn@2x"]]; [self.view addSubview:image2]; UIImageView* image3 = [[UIImageView alloc] init]; image3.frame = CGRectMake(150, 300, 140, 100); // [image3 setImage:[UIImage imageNamed:@"next_btn@2x"]]; [self.view addSubview:image3];
UILabel * label1 = [[UILabel alloc] init]; label1.frame = CGRectMake(0, 100, 140, 40); [label1 setTextAlignment:NSTextAlignmentRight]; [label1 setText:@"电影名称:"]; [label1 setFont:[UIFont systemFontOfSize:12]]; [label1 setTextColor:[UIColor blackColor]]; _label11 = [[UILabel alloc] init]; _label11.frame = CGRectMake(150, 100, 140, 40); [_label11 setTextAlignment:NSTextAlignmentCenter]; [_label11 setText:_movie.title]; [_label11 setFont:[UIFont systemFontOfSize:12]]; [_label11 setTextColor:[UIColor blackColor]]; UILabel * label2 = [[UILabel alloc] init]; label2.frame = CGRectMake(0, 150, 140, 40); [label2 setTextAlignment:NSTextAlignmentRight]; [label2 setText:@"票房数:"]; [label2 setFont:[UIFont systemFontOfSize:12]]; [label2 setTextColor:[UIColor blackColor]]; _label22 = [[UILabel alloc] init]; _label22.frame = CGRectMake(150, 150, 140, 40); [_label22 setTextAlignment:NSTextAlignmentCenter]; [_label22 setText:[NSString stringWithFormat:@"%@", _movie.boxOfficeGross]]; [_label22 setFont:[UIFont systemFontOfSize:12]]; [_label22 setTextColor:[UIColor blackColor]]; UILabel * label3 = [[UILabel alloc] init]; label3.frame = CGRectMake(0, 200, 320, 40); [label3 setTextAlignment:NSTextAlignmentCenter]; [label3 setText:@"内容简介:"]; [label3 setFont:[UIFont systemFontOfSize:12]]; [label3 setTextColor:[UIColor blackColor]]; _label33 = [[UILabel alloc] init]; _label33.frame = CGRectMake(0, 250, 320, 100); [_label33 setTextAlignment:NSTextAlignmentCenter]; [_label33 setText:_movie.summary]; _label33.numberOfLines=0;//自动换行 [_label33 setFont:[UIFont systemFontOfSize:12]]; [_label33 setTextColor:[UIColor blackColor]]; UIButton * btn = [[UIButton alloc] init]; btn.frame = CGRectMake(0, 400, 320, 50); [btn setTitle:@"编辑" forState:UIControlStateNormal]; [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [btn setBackgroundImage:[UIImage imageNamed:@"next_btn@2x"] forState:UIControlStateNormal]; [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; [self.view addSubview:_label11]; [self.view addSubview:_label22]; [self.view addSubview:_label33]; [self.view addSubview:label1]; [self.view addSubview:label2]; [self.view addSubview:label3]; }
<span style="font-family: Arial, Helvetica, sans-serif;">//<span style="white-space:pre"> </span>初始化Movie的一个对象,用于显示在第一第二页面。</span>
- (void)addclass { NSNumber * num=[NSNumber numberWithLong:1646123]; _movie = [Movie MovieWithTitle:@"复仇者联盟2" andNum:num andSum:@"《复仇者联盟2:奥创纪元》(Avengers: Age of Ultron)是漫威影业出品的一部科幻冒险电影,取材自漫威漫画,是漫威电影宇宙第11部电影。由乔斯·韦登执导,小罗伯特·唐尼、克里斯·埃文斯、克里斯·海姆斯沃斯、马克·鲁法洛、斯嘉丽·约翰逊和杰瑞米·雷纳主演,定于2015年5月1日北美上映。影片讲述当钢铁侠试图启动处于休眠状态的维持和平计划时,事情出了差错。于是,在地球面临生死存亡的紧急关头时,强大的超级英雄们挺身而出承担起拯救世界的重任,他们将阻止可怕的人工智能机器人“奥创”制定恐怖计划。"]; } @end
3,点击在根视图控制器中的“修改”按钮,进入EditViewController,并将 根视图的label内容显示在EditViewController的三个textField中。
#import <UIKit/UIKit.h> #import "Movie.h" @interface EditeViewController : UIViewController<UITextFieldDelegate,UITextViewDelegate> @property(nonatomic,strong)UITextField* text1; @property(nonatomic,strong)UITextField* text2; @property(nonatomic,strong)UITextView* text3; @property(nonatomic,retain)Movie*movie2; @end #import "EditeViewController.h" #import "MainViewController.h" @interface EditeViewController () @end @implementation EditeViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { } return self; } - (void)viewDidLoad { [super viewDidLoad]; [self _initUI];
<span style="font-family: Arial, Helvetica, sans-serif;">//<span style="white-space:pre"> </span>代理模式,在重写touch方式时,要遵循<</span><span class="s1" style="font-family: Arial, Helvetica, sans-serif;">UITextFieldDelegate</span><span class="s2" style="font-family: Arial, Helvetica, sans-serif;">,</span><span class="s1" style="font-family: Arial, Helvetica, sans-serif;">UITextViewDelegate</span><span class="s2" style="font-family: Arial, Helvetica, sans-serif;">>协议,再设置代理模式,达到点击控件textview时,界面上移下移的目的</span>
_text3.delegate = self; }
<span style="font-family: Arial, Helvetica, sans-serif;">// 初始化页面控件</span>
- (void)_initUI { UIButton * btn = [[UIButton alloc] init]; btn.frame = CGRectMake(0, 400, 320, 50); [btn setTitle:@"保存" forState:UIControlStateNormal]; [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [btn setBackgroundImage:[UIImage imageNamed:@"next_btn@2x"] forState:UIControlStateNormal]; [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; UILabel * label_text1 = [[UILabel alloc] init]; label_text1.frame = CGRectMake(0, 100, 140, 40); [label_text1 setTextAlignment:NSTextAlignmentCenter]; [label_text1 setText:@"电影名称:"]; [label_text1 setFont:[UIFont systemFontOfSize:12]]; [label_text1 setTextColor:[UIColor blueColor]]; [self.view addSubview:label_text1]; _text1 = [[UITextField alloc] init]; _text1.frame = CGRectMake(150, 100, 100, 40); _text1.borderStyle = UITextBorderStyleRoundedRect; [_text1 setTextAlignment:NSTextAlignmentCenter]; _text1.placeholder = @"请输入"; _movie2.title = _text1.text; [self.view addSubview:_text1]; UILabel * label_text2 = [[UILabel alloc] init]; label_text2.frame = CGRectMake(0, 200, 140, 40); [label_text2 setTextAlignment:NSTextAlignmentCenter]; [label_text2 setText:@"当前票房纪录:"]; [label_text2 setFont:[UIFont systemFontOfSize:12]]; [label_text2 setTextColor:[UIColor blueColor]]; [self.view addSubview:label_text2]; _text2 = [[UITextField alloc] init]; _text2.frame = CGRectMake(150, 200, 100, 40); // text1.background = [UIImage imageNamed:@"next_btn@2x"]; _text2.borderStyle = UITextBorderStyleRoundedRect; [_text2 setTextAlignment:NSTextAlignmentCenter]; _text2.placeholder = @"请输入"; // _movie2.boxOfficeGross = _text2.text; [self.view addSubview:_text2]; UILabel * label_text3 = [[UILabel alloc] init]; label_text3.frame = CGRectMake(0, 300, 140, 40); [label_text3 setTextAlignment:NSTextAlignmentCenter]; [label_text3 setText:@"影片简介:"]; [label_text3 setFont:[UIFont systemFontOfSize:12]]; [label_text3 setTextColor:[UIColor blueColor]]; [self.view addSubview:label_text3]; _text3 = [[UITextView alloc] init]; _text3.frame = CGRectMake(150, 300, 100, 40); [_text3 setTextAlignment:NSTextAlignmentCenter]; _movie2.summary = _text3.text; [self.view addSubview:_text3]; } <pre name="code" class="objc"><span style="font-family: Arial, Helvetica, sans-serif;">// 开始对textview编辑后,点击textview控件,自动调用该方法</span>- (void)textViewDidBeginEditing:(UITextView *)textView{ [UIView animateWithDuration:0.3 animations:^{ self.view.frame = CGRectMake(0, -200, 320, 480); }];}
<span style="font-family: Arial, Helvetica, sans-serif;">// 结束对textview编辑后,点击空白,自动调用该方法</span>
- (void)textViewDidEndEditing:(UITextView *)textView { self.view.frame = CGRectMake(0,0, 320, 480); } - (void)textFieldDidBeginEditing:(UITextField *)textField { [UIView animateWithDuration:0.3 animations:^{ self.view.frame = CGRectMake(0, -200, 320, 480); }]; } - (void)textFieldDidEndEditing:(UITextField *)textField { self.view.frame = CGRectMake(0,0, 320, 480); }
//#pragma mark 触控事件,点击textField时,隐藏键盘 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [_text1 resignFirstResponder]; <span style="font-family: Arial, Helvetica, sans-serif;">//点击_text1后,再次点击空白处隐藏键盘</span> [_text2 resignFirstResponder]; <span style="font-family: Arial, Helvetica, sans-serif;">//点击_text2</span><span style="font-family: Arial, Helvetica, sans-serif;">后,再次点击空白处</span><span style="font-family: Arial, Helvetica, sans-serif;">隐藏键盘 </span> [_text3 resignFirstResponder]; <span style="font-family: Arial, Helvetica, sans-serif;">//点击_text3</span><span style="font-family: Arial, Helvetica, sans-serif;">后,再次点击空白处</span><span style="font-family: Arial, Helvetica, sans-serif;">隐藏键盘</span><span style="font-family: Arial, Helvetica, sans-serif;"> </span>}
<span style="font-family: Arial, Helvetica, sans-serif;">//<span style="white-space:pre"> </span>点击_text1控件编辑结束后,点击键盘中的Return键时,实现隐藏键盘的目的</span>
- (BOOL)textFieldShouldReturn:(UITextField *)textField { [_text1 resignFirstResponder]; return YES; } - (void)click:(id)sender {
<span style="font-family: Arial, Helvetica, sans-serif;">//<span style="white-space:pre"> </span>实现第二页面向第一页面传值的操作,因为第一,第二页面都定义了一个Movie的属性,利用retain浅拷贝实现第二页面修改,第一页面显示修改后的内容</span>
_movie2.title = _text1.text; _movie2.boxOfficeGross = [NSNumber numberWithLong:[_text2.text integerValue]]; _movie2.summary = _text3.text;
<span style="font-family: Arial, Helvetica, sans-serif;">//<span style="white-space:pre"> </span>跳转到上一个页面</span>
[self dismissViewControllerAnimated:YES completion:nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end
4,修改DetailViewController的textField中的值并返回到根视图时,label的值相 应改变。
相关文章推荐
- 致PHP路上的“年轻人”
- 改变navigationbar 高度, 并且让tableview的cell 不被覆盖
- linux fdisk tf卡分区操作解析说明
- Spring MVC ContentNegotiatingViewResolver 样例
- DevExpress使用教程:使用ChartControl绘制多重坐标图形
- 利用矢量计算快速判定一点在直线的哪一侧
- 关于iOS开发
- 程序员面试笔试题目-素数、atoi、itoa、strstr、strcpy以及两个数交换(一)
- 文章标题
- memcached使用详解
- P,MTHBGWB - 1051
- wamp默认函数嵌套98层,否则报Fatal error: Maximum function nesting level of '100' reached, aborting!
- 国内最大的 Node.js 社区将 New Relic 的监控产品换成了 OneAPM
- nfs只能挂载为nobody的解决方法
- WinForm雷达动态显示
- MySQL 的主从 Replication同步
- View机制深入学习(二)——View树的遍历
- DATAGUARD安装
- android 9PNG图片制作
- 透明