自己编写不免会有疏忽,如有错误请多多指正
首先创建工程,这里我们采用纯手写代码方式,新建俩个UIViewController的子类文件,分别是:FirstViewController、SecondViewControlle。之后声明一个类文件movie用来存放电影的属性。
注:下面空间的坐标适合iPhone6s plus,如果想要适用于其他的手机需要进行适配,我会在以后的文章中进行补充。
然后进行编写,下面是代码:
movie.h文件
#import <Foundation/Foundation.h>
@interface Movie : NSObject
{
NSString *title;//电影名
NSNumber *money;//票房
NSString *descrip;//简介
}
@property(nonatomic,retain) NSString *title;
@property(nonatomic,retain) NSNumber *money;
@property(nonatomic,retain) NSString *descrip;
-(id)initWithTitle:(NSString*)_t andMoney:(NSNumber*)_m andDescrip:(NSString*)_d;
@end
movie.m文件
#import "movie.h"
@implementation Movie
@synthesize title,money,descrip;
//初始化方法
-(id)initWithTitle:(NSString*)_t andMoney:(NSNumber*)_m andDescrip:(NSString*)_d
{
if (self = [super init]) {
self.title = _t;
self.money = _m;
self.descrip = _d;
}
return self;
}
@end
FirstViewController.h文件:
#import <UIKit/UIKit.h>
#import "movie.h"
@interface FirstViewController : UIViewController
{
Movie * movie;
UILabel *label5;//用来显示电影名称
UILabel *label6;//电影票房
UITextView *firstTextView;//电影简介
}
@property (nonatomic,retain) UILabel *label5;
@property (nonatomic,retain) UILabel *label6;
@property (nonatomic,retain) UITextView *firstTextView;
@end
FirstViewController.m文件
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
@synthesize label5,label6,firstTextView;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//创建标签
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(80, 50, 200, 100)];
label1.text = @"电影简介";
label1.font = [UIFont systemFontOfSize:50];
label1.textAlignment = NSTextAlignmentCenter;
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(5, 130, 200, 100)];
label2.text = @"名字:";
label2.font = [UIFont systemFontOfSize:30];
label2.textAlignment = NSTextAlignmentCenter;
UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(5, 180, 200, 100)];
label3.text = @"票房:";
label3.font = [UIFont systemFontOfSize:30];
label3.textAlignment = NSTextAlignmentCenter;
UILabel *label4 = [[UILabel alloc]initWithFrame:CGRectMake(5, 230, 200, 100)];
label4.text = @"简介:";
label4.font = [UIFont systemFontOfSize:30];
label4.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:label1];
[self.view addSubview:label2];
[self.view addSubview:label3];
[self.view addSubview:label4];
//初始化movie
NSNumber *num = [NSNumber numberWithInteger:123456];
movie = [[Movie alloc]initWithTitle:@"疯狂动物城" andMoney:num andDescrip:@"非常好看,无法形容,烂透了!!!!!!!"];
label5 = [[UILabel alloc]initWithFrame:CGRectMake(110, 130, 200, 100)];
label5.text = movie.title;
label5.font = [UIFont systemFontOfSize:30];
label5.textAlignment = NSTextAlignmentCenter;
label6 = [[UILabel alloc]initWithFrame:CGRectMake(135, 180, 200, 100)];
label6.text = [NSString stringWithFormat:@"%@",movie.money];
label6.font = [UIFont systemFontOfSize:30];
label6.textAlignment = NSTextAlignmentLeft;
//设置显示行数
//label6.numberOfLines = 0;//0是不限制
firstTextView = [[UITextView alloc]initWithFrame:CGRectMake(140, 255, 220, 350)];
firstTextView.text = movie.descrip;
firstTextView.font = [UIFont systemFontOfSize:25];
[firstTextView setBackgroundColor:[UIColor grayColor]];
//让firstTextView不可修改
firstTextView.editable = NO;
[self.view addSubview:label5];
[self.view addSubview:label6];
[self.view addSubview:firstTextView];
//创建编辑按钮
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(130, 600, 150, 90)];
[btn setTitle:@"编辑" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
btn.titleLabel.font = [UIFont systemFontOfSize:40];
//绑定方法
[btn addTarget:self action:@selector(editmethod:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
//编辑按钮的方法
-(void)editmethod:(id)sender
{
//声明第二个界面
SecondViewController *secondView = [[SecondViewController alloc]init];
//将第二界面使用第一界面的数据
secondView.mv = movie;
secondView.view.backgroundColor = [UIColor whiteColor];
//跳转到第二个界面
[self presentViewController:secondView animated:YES completion:nil];
}
//显示第一个界面之前进行刷新
-(void)viewWillAppear:(BOOL)animated
{
label5.text = movie.title;
label6.text = [NSString stringWithFormat:@"%@",movie.money];
firstTextView.text = movie.descrip;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
SecondViewController.h文件
#import <UIKit/UIKit.h>
#import "movie.h"
@interface SecondViewController : UIViewController
{
UITextField *texFile1;
UITextField *texFile2;
UITextView *seTextView;
}
@property (nonatomic,retain) Movie *mv;
@end
SecondViewController.m文件
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
@synthesize mv;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//创建标签
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(80, 50, 200, 100)];
label1.text = @"电影简介";
label1.font = [UIFont systemFontOfSize:50];
label1.textAlignment = NSTextAlignmentCenter;
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(5, 130, 200, 100)];
label2.text = @"名字:";
label2.font = [UIFont systemFontOfSize:30];
label2.textAlignment = NSTextAlignmentCenter;
UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(5, 180, 200, 100)];
label3.text = @"票房:";
label3.font = [UIFont systemFontOfSize:30];
label3.textAlignment = NSTextAlignmentCenter;
UILabel *label4 = [[UILabel alloc]initWithFrame:CGRectMake(5, 230, 200, 100)];
label4.text = @"简介:";
label4.font = [UIFont systemFontOfSize:30];
label4.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:label1];
[self.view addSubview:label2];
[self.view addSubview:label3];
[self.view addSubview:label4];
//文本框设置
texFile1 = [[UITextField alloc]initWithFrame:CGRectMake(140, 160, 220, 40)];
texFile1.text = mv.title;
texFile1.textColor = [UIColor orangeColor];
//输入框的风格
texFile1.borderStyle = UITextBorderStyleLine;
//默认显示的文字
texFile1.placeholder = @"请输入电影名称";
texFile2 = [[UITextField alloc]initWithFrame:CGRectMake(140, 210, 220, 40)];
texFile2.text = [NSString stringWithFormat:@"%@",mv.money];
texFile2.textColor = [UIColor orangeColor];
//输入框的风格
texFile2.borderStyle = UITextBorderStyleLine;
//默认显示的文字
texFile2.placeholder = @"请输入票房";
seTextView = [[UITextView alloc]initWithFrame:CGRectMake(140, 255, 220, 350)];
seTextView.text = mv.descrip;
seTextView.textColor = [UIColor orangeColor];
seTextView.font = [UIFont systemFontOfSize:25];
[self.view addSubview:texFile1];
[self.view addSubview:texFile2];
[self.view addSubview:seTextView];
//创建返回按钮
UIButton *returnBtn = [[UIButton alloc]initWithFrame:CGRectMake(130, 600, 150, 90)];
[returnBtn setTitle:@"返回" forState:UIControlStateNormal];
[returnBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
returnBtn.titleLabel.font = [UIFont systemFontOfSize:40];
//绑定方法
[returnBtn addTarget:self action:@selector(returnmethod:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:returnBtn];
}
//返回按钮的方法
-(void)returnmethod:(id)sender
{
mv.title = texFile1.text;
mv.money = [NSNumber numberWithInteger:[texFile2.text intValue]];
mv.descrip = seTextView.text;
[self dismissViewControllerAnimated:YES completion:nil];
}
//点击空白的地方响应
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//键盘消失
[texFile1 resignFirstResponder];
[texFile2 resignFirstResponder];
[seTextView resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
本篇文章就写到这里,欢迎大家采纳,建议。