今天的内容是昨天的跟进版,对部分内容作了优化,关于屏幕适配的问题,做了简单的处理,即使用屏幕比例来布局,欢迎指正!(同样是纯手写代码)
首先先来展示一下效果,运行时第一个界面:
进行修改:
点击确认:
下面是具体的代码:
注:要先把movie类及图片素材添加好。
AppDelegate.m文件
#import "AppDelegate.h"
#import "FirstViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL )application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self .window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
FirstViewController *firstView = [[FirstViewController alloc]init];
UINavigationController * navigation = [[UINavigationController alloc]initWithRootViewController:firstView];
self .window .rootViewController = navigation;
self .window .backgroundColor = [UIColor grayColor];
UIImageView * imageView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen]bounds]];
imageView.image = [UIImage imageNamed:@"image.jpg" ];
[self .window addSubview:imageView];
[self .window makeKeyAndVisible];
return YES ;
}
- (void )applicationWillResignActive:(UIApplication *)application {
}
- (void )applicationDidEnterBackground:(UIApplication *)application {
}
- (void )applicationWillEnterForeground:(UIApplication *)application {
}
- (void )applicationDidBecomeActive:(UIApplication *)application {
}
- (void )applicationWillTerminate:(UIApplication *)application {
}
@end
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];
CGRect rect = [[UIScreen mainScreen]bounds];
CGFloat x = rect.size .width ;
CGFloat y = rect.size .height ;
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(0.18 *x, 0.07 *y, 0.7 *x , 0.2 *y)];
label1.text = @"电影简介" ;
label1.font = [UIFont systemFontOfSize:0.12 *x];
label1.textAlignment = NSTextAlignmentCenter;
label1.textColor = [UIColor yellowColor];
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(0.1 *x, 0.25 *y, 0.25 *x, 0.06 *y)];
label2.text = @"名字:" ;
label2.font = [UIFont systemFontOfSize:0.08 *x];
label2.textAlignment = NSTextAlignmentCenter;
label2.textColor = [UIColor yellowColor];
UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(0.1 *x, 0.35 *y, 0.25 *x, 0.06 *y)];
label3.text = @"票房:" ;
label3.font = [UIFont systemFontOfSize:0.08 *x];
label3.textAlignment = NSTextAlignmentCenter;
label3.textColor = [UIColor yellowColor];
UILabel *label4 = [[UILabel alloc]initWithFrame:CGRectMake(0.1 *x, 0.45 *y, 0.25 *x, 0.06 *y)];
label4.text = @"简介:" ;
label4.font = [UIFont systemFontOfSize:0.08 *x];
label4.textAlignment = NSTextAlignmentCenter;
label4.textColor = [UIColor yellowColor];
[self .view addSubview:label1];
[self .view addSubview:label2];
[self .view addSubview:label3];
[self .view addSubview:label4];
NSNumber *num = [NSNumber numberWithInteger:123456 ];
movie = [[Movie alloc]initWithTitle:@"疯狂动物城" andMoney:num andDescrip:@"非常好看,无法形容,值得大家去观赏!" ];
label5 = [[UILabel alloc]initWithFrame:CGRectMake(0.3 *x, 0.25 *y, 0.5 *x, 0.06 *y)];
label5.text = movie.title ;
label5.font = [UIFont systemFontOfSize:0.08 *x];
label5.textAlignment = NSTextAlignmentCenter;
label5.textColor = [UIColor orangeColor];
label6 = [[UILabel alloc]initWithFrame:CGRectMake(0.34 *x, 0.35 *y, 0.5 *x, 0.06 *y)];
label6.text = [NSString stringWithFormat:@"%@" ,movie.money ];
label6.font = [UIFont systemFontOfSize:0.08 *x];
label6.textAlignment = NSTextAlignmentLeft;
label6.textColor = [UIColor orangeColor];
firstTextView = [[UITextView alloc]initWithFrame:CGRectMake(0.33 *x, 0.45 *y,0.6 *x, 0.5 *y)];
firstTextView.text = movie.descrip ;
firstTextView.font = [UIFont systemFontOfSize:0.06 *x];
firstTextView.textColor = [UIColor orangeColor];
firstTextView.editable = NO ;
[firstTextView setBackgroundColor:[UIColor clearColor]];
[self .view addSubview:label5];
[self .view addSubview:label6];
[self .view addSubview:firstTextView];
self .navigationItem .title = @"电影详情" ;
[self .navigationController .navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:0.08 *x],NSForegroundColorAttributeName:[UIColor redColor]}];
[self .navigationController .navigationBar setBackgroundImage:[UIImage imageNamed:@"image.jpg" ] forBarMetrics:UIBarMetricsDefault];
self .navigationItem .rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector (rightBtn:)];
NSMutableDictionary *rightAttrs = [NSMutableDictionary dictionary];
rightAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:0.06 *x];
rightAttrs[NSForegroundColorAttributeName] = [UIColor redColor];
[self .navigationItem .rightBarButtonItem setTitleTextAttributes:rightAttrs forState:UIControlStateNormal];
}
-(void )rightBtn:(id )sender
{
SecondViewController *secondView = [[SecondViewController alloc]init];
secondView.mv = movie;
[self .navigationController pushViewController:secondView animated:YES ];
}
-(void )viewWillAppear:(BOOL )animated
{
label5.text = movie.title ;
label6.text = [NSString stringWithFormat:@"%@" ,movie.money ];
firstTextView.text = movie.descrip ;
}
- (void )didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@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];
CGRect rect = [[UIScreen mainScreen]bounds];
CGFloat x = rect.size .width ;
CGFloat y = rect.size .height ;
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(0.18 *x, 0.07 *y, 0.7 *x , 0.2 *y)];
label1.text = @"电影简介" ;
label1.font = [UIFont systemFontOfSize:0.12 *x];
label1.textAlignment = NSTextAlignmentCenter;
label1.textColor = [UIColor yellowColor];
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(0.1 *x, 0.25 *y, 0.25 *x, 0.06 *y)];
label2.text = @"名字:" ;
label2.font = [UIFont systemFontOfSize:0.08 *x];
label2.textAlignment = NSTextAlignmentCenter;
label2.textColor = [UIColor yellowColor];
UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(0.1 *x, 0.35 *y, 0.25 *x, 0.06 *y)];
label3.text = @"票房:" ;
label3.font = [UIFont systemFontOfSize:0.08 *x];
label3.textAlignment = NSTextAlignmentCenter;
label3.textColor = [UIColor yellowColor];
UILabel *label4 = [[UILabel alloc]initWithFrame:CGRectMake(0.1 *x, 0.45 *y, 0.25 *x, 0.06 *y)];
label4.text = @"简介:" ;
label4.font = [UIFont systemFontOfSize:0.08 *x];
label4.textAlignment = NSTextAlignmentCenter;
label4.textColor = [UIColor yellowColor];
[self .view addSubview:label1];
[self .view addSubview:label2];
[self .view addSubview:label3];
[self .view addSubview:label4];
texFile1 = [[UITextField alloc]initWithFrame:CGRectMake(0.34 *x, 0.26 *y, 0.5 *x, 0.06 *y)];
texFile1.text = mv.title ;
texFile1.textColor = [UIColor orangeColor];
texFile1.font = [UIFont systemFontOfSize:0.06 *x];
texFile1.borderStyle = UITextBorderStyleLine;
texFile1.placeholder = @"请输入电影名称" ;
[texFile1 setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor" ];
texFile2 = [[UITextField alloc]initWithFrame:CGRectMake(0.34 *x, 0.36 *y, 0.5 *x, 0.06 *y)];
texFile2.text = [NSString stringWithFormat:@"%@" ,mv.money ];
texFile2.textColor = [UIColor orangeColor];
texFile2.font = [UIFont systemFontOfSize:0.06 *x];
texFile2.borderStyle = UITextBorderStyleLine;
texFile2.placeholder = @"请输入票房" ;
[texFile2 setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor" ];
seTextView = [[UITextView alloc]initWithFrame:CGRectMake(0.33 *x, 0.46 *y,0.6 *x, 0.5 *y)];
seTextView.text = mv.descrip ;
seTextView.textColor = [UIColor orangeColor];
seTextView.font = [UIFont systemFontOfSize:0.06 *x];
[seTextView setBackgroundColor:[UIColor clearColor]];
[self .view addSubview:texFile1];
[self .view addSubview:texFile2];
[self .view addSubview:seTextView];
self .navigationItem .title = @"编辑信息" ;
self .navigationItem .rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"确认" style:UIBarButtonItemStyleDone target:self action:@selector (rightBtn:)];
NSMutableDictionary *leftAttrs = [NSMutableDictionary dictionary];
leftAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:0.06 *x];
leftAttrs[NSForegroundColorAttributeName] = [UIColor redColor];
[self .navigationItem .rightBarButtonItem setTitleTextAttributes:leftAttrs forState:UIControlStateNormal];
self .navigationItem .leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector (leftBtn:)];
NSMutableDictionary *rightAttrs = [NSMutableDictionary dictionary];
rightAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:0.06 *x];
rightAttrs[NSForegroundColorAttributeName] = [UIColor redColor];
[self .navigationItem .leftBarButtonItem setTitleTextAttributes:rightAttrs forState:UIControlStateNormal];
}
-(void )rightBtn:(id )sender
{
mv.title = texFile1.text ;
mv.money = [NSNumber numberWithInteger:texFile2.text .integerValue ];
mv.descrip = seTextView.text ;
[self .navigationController popViewControllerAnimated:YES ];
}
-(void )leftBtn:(id )sender
{
[self .navigationController popViewControllerAnimated:YES ];
}
-(void )touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[texFile1 resignFirstResponder];
[texFile2 resignFirstResponder];
[seTextView resignFirstResponder];
}
- (void )didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end