导航栏来显示简单的电影简介

来源:互联网 发布:皓思网络咨询有限公司 编辑:程序博客网 时间:2024/05/29 10:06

今天的内容是昨天的跟进版,对部分内容作了优化,关于屏幕适配的问题,做了简单的处理,即使用屏幕比例来布局,欢迎指正!(同样是纯手写代码)

首先先来展示一下效果,运行时第一个界面:

初始化界面

进行修改:

修改界面

点击确认:

编辑之后的界面

下面是具体的代码:

注:要先把movie类及图片素材添加好。

AppDelegate.m文件

#import "AppDelegate.h"#import "FirstViewController.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];    FirstViewController *firstView = [[FirstViewController alloc]init];    //导航控制器(它本身没有视图,故将firstview设置为它的根视图)    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 {    // 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

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.    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];    //初始化movie    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];    //设置显示行数    //label6.numberOfLines = 0;//0是不限制    //简介用文本视图显示    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不可修改    firstTextView.editable = NO;    //设置firstTextView背景透明    [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];    // 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.    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背景透明    [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];    // 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
0 0
原创粉丝点击