`

[iOS开发教程-3]Create a Detail View Page using UIImageView, UITextView and UILabel

    博客分类:
  • iOS
 
阅读更多

http://www.iphonedevcentral.com/create-a-detail-view-page-using-uiimageview-uitextview-and-uilabel/

 

Create a Detail View Page using UIImageView, UITextView and UILabel

tutorial019

I’m going to build this tutorial on the previous two so if you haven’t checked them out yet, you can see the first one here and the second one here . You can also download the source code on the bottom of each tutorial.

What we’ve done so far

We used UITableViewController to build out our root view controller that lists all the DVDs from our data file. Then we customized each cell by adding a DVD cover and some basic info about each movie. Tapping a row in the table caused the app to go to a detail page which was also a table displaying expanded info about the selected movie. That’s the part we’re going to work on now.

1. Download some source code to get your started

Since we’re going to be completely removing the detail view controller we used in the previous tutorials, let’s just start from there. Instead of walking you through deleting the controller and removing the appropriate functionality, download this source code that will get you started. You’ll get the root view controller with customized cells in it. However, tapping on any of the rows won’t do anything, yet. Download the primer project here: MyDVDLibrary03Primer .

2. Enhance test data file

Since our test data file only contains title, length, image and release date for each movie, that’s not going to cut it when trying to design a detail view page. We’ll at least want to add the description of the movie and maybe also its genre. This can be very tedious to do in a plist like we’re using right now but it will get a lot easier once we start using Core Data that I will cover in later tutorials.

I won’t make you add all the data by hand so for a shortcut, download the completed plist file here and replace your existing one that can be found in Xcode under the Resources folder: TestData.plist .

3. Create new DetailViewController class

In Xcode’s file browser on the left, right-click on the Classes folder and choose Add -> New File… . Under Cocoa Touch Classes group choose UIViewController subclass and make sure to check the With XIB for user interface checkbox. This will not only create our subclass but also the NIB file that we’ll use to layout our UI components.


tutorial015

 

4. Set up UI outlets

We now need to set up all properties in our controller that will be used on the detail view page. There will be an image view for the DVD cover, a label for the title, release date, length, genre and a text view for the description. Add this to the header file of the DetailViewController (DetailViewController.h ):

@interface DetailViewController : UIViewController {
    IBOutlet UIImageView *coverImageView;
    IBOutlet UILabel *titleLabel;
    IBOutlet UILabel *releaseDateLabel;
    IBOutlet UILabel *lengthLabel;
    IBOutlet UILabel *genreLabel;
    IBOutlet UITextView *descriptionTextView;
}
 
@property (nonatomic, retain) UIImageView *coverImageView;
@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) UILabel *releaseDateLabel;
@property (nonatomic, retain) UILabel *lengthLabel;
@property (nonatomic, retain) UILabel *genreLabel;
@property (nonatomic, retain) UITextView *descriptionTextView;
 
@end

Synthesize the properties in the implementation file (DetailViewController.m ):

@synthesize coverImageView, titleLabel, releaseDateLabel, lengthLabel, genreLabel, descriptionTextView;

5. Lay out and connect UI components in Interface Builder

Before we jump to Interface Builder make sure to save all the changes so that IB can pick up on them.

In Xcode, locate DetailViewController.xib file (it got created this file along with DetailViewController class in Step#3) and double-click on it. Once Interface Builder opens you should see an empty canvas where we’re going to place our UI elements. Lay them out as shown in the screenshot below. Note that you should uncheck Editable checkbox on your text view so that the text is read-only.

tutorial016

Now we need to connect the UI elements to its variable counterparts. I’m not going to cover step-by-step how to do that as it has been discussed in some detail in the tutorial on customizing view cells . Please, review that tutorial if you’re unclear how to follow the next set of instructions.

Click on File’s Owner icon to highlight it. From the file menu choose Tools -> Connections Inspector (Cmd + 2). Under Outlets , you’ll see all of our instance variables we declared in the DetailViewController class. Control-drag from File’s Owner to each UI component in the view to connect coverImageView to Image View , releaseDateLabel to Release Date label, genreLabel to Genre label, etc. Once done, your Connections Inspector should be set up like this:

tutorial017

Save your changes and return back to Xcode.

6. Prepare DetailViewController to read test data

If you recall what we did with the original DetailViewController when it was still a table, in its initialization method we passed in a dictionary containing all the info about our DVD library. This was read in using DvdLibraryDao object that basically took the plist we constructed earlier and put all its content into a dictionary. We’re going to do the same thing in our new DetailViewController , only we’re now adding a couple of properties (genre, description) that we just added in Step#2.

We also add declaration of our custom initialization method here. It’s pretty much just a modified initWithNibName method that adds dvdData into the mix. This method will set the local instance variable dvdData that is also declared here. Change DetailViewController.h header file to look like this:

@interface DetailViewController : UIViewController {
    IBOutlet UIImageView *coverImageView;
    IBOutlet UILabel *titleLabel;
    IBOutlet UILabel *releaseDateLabel;
    IBOutlet UILabel *lengthLabel;
    IBOutlet UILabel *genreLabel;
    IBOutlet UITextView *descriptionTextView;
 
    NSDictionary *dvdData;
}
 
@property (nonatomic, retain) UIImageView *coverImageView;
@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) UILabel *releaseDateLabel;
@property (nonatomic, retain) UILabel *lengthLabel;
@property (nonatomic, retain) UILabel *genreLabel;
@property (nonatomic, retain) UITextView *descriptionTextView;
 
- (id)initWithDvdData:(NSDictionary *)dvdData nibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;
 
@end

Switch over to DetailViewController.m implementation file. Let’s implement the initWithDvdData method:

- (id)initWithDvdData:(NSDictionary *)data nibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        dvdData = data;
    }
    return self;
}

All this is doing is it assigns data that is passed in to our instance variable dvdData .

To read everything in from dvdData to all of the UI elements, we’ll insert a little snippet of code in the viewDidLoad delegate method. As you can see, we assign each value from dvdData to its corresponding UI element.

- (void)viewDidLoad {
    coverImageView.image = [UIImage imageNamed:[dvdData valueForKey:@"coverImage"]];
    titleLabel.text = [dvdData valueForKey:@"title"];
    releaseDateLabel.text = [NSString stringWithFormat:@"%@", [dvdData valueForKey:@"releaseDate"]];
    lengthLabel.text = [NSString stringWithFormat:@"%@ minutes", [dvdData valueForKey:@"featureLength"]];
    genreLabel.text = [dvdData valueForKey:@"genre"];
    descriptionTextView.text = [dvdData valueForKey:@"description"];
    [super viewDidLoad];
}

7. Handle didSelectRowAtIndexPath in RootViewController

Now that we have our detail view controller all ready to go, let’s connect the dots and have the RootViewController go to the detail page when a row is tapped.

In RootViewController.m, locate the didSelectRowAtIndexPath method. Just as we did before, we’ll instantiate DetailViewController here and push it to the controller stack:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DetailViewController *controller = [[DetailViewController alloc]
                                        initWithDvdData:[dao libraryItemAtIndex:indexPath.row]
                                        nibName:@"DetailViewController" bundle:[NSBundle mainBundle]];
    controller.title = [[dao libraryItemAtIndex:indexPath.row] valueForKey:@"title"];
    [self.navigationController pushViewController:controller animated:YES];
    [controller release];
}

If everything went well, you should be able to run your project and get something like this:

tutorial018

8. Format the release date field

While something like 2007-07-08 16:32:07 -0700 is completely acceptable by me, many people want to see something a bit more user friendly. Let’s take that date and try to display it as July 8, 2007 .  We’re going to achieve that using a nifty utility class called NSDateFormatter. Open DetailViewController.m and change viewDidLoad method to look like this:

- (void)viewDidLoad {
    coverImageView.image = [UIImage imageNamed:[dvdData valueForKey:@"coverImage"]];
    titleLabel.text = [dvdData valueForKey:@"title"];
    lengthLabel.text = [NSString stringWithFormat:@"%@ minutes", [dvdData valueForKey:@"featureLength"]];
    genreLabel.text = [dvdData valueForKey:@"genre"];
    descriptionTextView.text = [dvdData valueForKey:@"description"];
 
    // Format date we get from releaseDate field to more readable form
    NSDate *releaseDate = (NSDate *)[dvdData valueForKey:@"releaseDate"];
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setDateFormat:@"MMMM d, YYYY"];
    releaseDateLabel.text = [dateFormatter stringFromDate:releaseDate];
 
    [super viewDidLoad];
}

All we had done is gave our formatter a string representation of the date format we want and then outputted that date to releaseDateLabel .

Conclusion

When customizing your detail views, possibilities are endless. You could add a texture to your background, add more images, maybe a table with some detailed information, or other controls that would take you forward in the controller stack. But I hope this tutorial got you started and showed you some basic techniques and tricks to create a detail page.

You can download the complete source code to this tutorial here: My DVD Library Tutorial 3

tutorial019

 

 

 

分享到:
评论

相关推荐

    iOS控件 -- UIImageView使用详解

    在iOS开发中,UIImageView是用于显示图像的UI控件,它是UIKit框架的一部分。这个控件在用户界面设计中扮演着重要角色,无论是显示应用图标、背景图片还是用户头像,UIImageView都是不可或缺的元素。本篇文章将深入...

    iOS开发基础教程-源代码.rar

    这个“iOS开发基础教程-源代码.rar”文件显然是一份教学资料,包含了一些基本的iOS应用程序开发的源代码示例。通过学习这些源代码,开发者可以更好地理解iOS编程的核心概念,包括Swift语言、Xcode集成开发环境(IDE...

    ios-HXFlexoView 一个非常牛B的View,实现一行多种样式的UILabel UIImageView混排.zip

    它基本上满足你日常大部分需求,不管是一行一个UILabel还是2个UILabel,或者是一个UIImageView或者2个,或者UILabel与UIImageView组合,只要你想的到的,HXFlexoView都可以满足你.如果你觉得对你有用,记得给点个赞哦,谢谢...

    IOS开发实例-2、进度条、等待动画开始停止

    在iOS开发中,进度条(Progress Bar)和等待动画(Loading Animation)是用户界面中常见的元素,用于向用户展示操作的进度或等待状态。在本实例中,我们将深入探讨如何在iOS应用中实现这两个功能。 首先,让我们...

    ios-UIImageView加载SVG格式的图片.zip

    在iOS开发中,SVG(Scalable Vector Graphics)是一种常用于创建高质量、可缩放的图形格式。相较于传统的位图图像(如JPEG或PNG),SVG图像在放大时不会出现像素化,因此在需要高分辨率显示或者图形元素需要动态改变...

    ios-XMNRotateScaleView-一个单指缩放,旋转的View.zip

    一个单手手势旋转放大的自定义View,可以添加UIImageView,UILabel作为其contentView,实现其delegate,可以实现UILabel在放大的同时字体也放大 github地址:https://github.com/ws00801526/XMNViews

    iOS-ui-汤姆猫

    在iOS开发中,UI设计是至关重要的一环,它关乎到应用程序的用户体验和视觉吸引力。"iOS-ui-汤姆猫"这个主题可能是一个教学资源或示例项目,专注于讲解如何使用iOS的基础UI控件来创建一个类似“汤姆猫”游戏的用户...

    ios小项目-卡牌游戏

    - Model-View-Controller模式是iOS开发中的常见架构,Model负责存储和处理数据,View负责展示,Controller作为桥梁协调两者。 6. **Game Center集成**: - 若想增加竞争性,可以集成Apple的Game Center,提供排行...

    Programming iOS 12: Dive Deep into Views, View Controllers, and Frameworks

    《Programming iOS 12: Dive Deep into Views, View Controllers, and Frameworks》是一本专为进阶学习者设计的书籍,旨在深入探索苹果iOS 12操作系统中的视图、视图控制器以及各种框架的编程技术。这本书涵盖了iOS...

    iOS开发 - 第01篇 - UI基础 - 04 - 超级猜图

    在本教程中,我们将深入探讨iOS开发中的UI基础,特别是通过一个名为“超级猜图”的小程序来学习。这个小程序是一个适合初学者的项目,旨在帮助开发者掌握基本的用户界面设计和交互实现。在这个项目的实践中,我们...

    JTImageLabel, 在iOS上,JTImageLabel同时保留UILabel和 UIImageView.zip

    JTImageLabel, 在iOS上,JTImageLabel同时保留UILabel和 UIImageView JTImageLabel JTImageLabel是一个简单的视图,它包含一个 UILabel 和一个 UIImageView,即使你更改了 UILabel的对齐方式。安装使用 CocoaPods...

    ios-tableHeaderScale-表头滑动缩放.zip

    在iOS开发中,创建具有吸引力和交互性的用户界面是至关重要的。"ios-tableHeaderScale-表头滑动缩放.zip" 提供了一个很好的示例,它展示了如何在表格(UITableView)中实现表头(Table Header)的滑动缩放动画效果。...

    ios-UIImageView帧动画.zip

    在iOS开发中,UIImageView是用于显示图像的常见控件,它可以用来展示静态图片,但同时,通过巧妙地利用帧动画,我们还能让它显示一系列连续的图像,从而创建出动态效果。这个“ios-UIImageView帧动画.zip”文件很...

    ios开发教程,iphone开发教程,苹果开发教程

    这篇“iOS开发教程”涵盖了从入门到精通的全过程,适合初学者和有一定经验的开发者参考。 Swift是Apple于2014年推出的编程语言,它以其简洁、安全和高性能的特点迅速获得了开发者们的喜爱。Swift语法清晰,易于学习...

    iOS设计模式-生成器

    在iOS开发中,设计模式是一种解决常见编程问题的模板或最佳实践,被广泛应用于构建可扩展、可维护的代码库。生成器(Builder)设计模式是设计模式中的一种,它将对象的创建过程抽象化,使得同样的创建过程可以创建...

    iOS-Apprentice-v7.0-Swift4.2.zip

    《iOS Apprentice v7.0 Swift 4.2》是一份专为初学者设计的iOS开发教程,基于Swift 4.2编程语言。这份教程旨在帮助新手快速掌握iOS应用开发的基本概念和技术,通过实际操作和详细解释,使学习者能够逐步构建自己的...

    The iOS Apprentice 1 - Getting Started

    《iOS Apprentice 1 - Getting Started》是一本专为初学者设计的iOS开发指南,主要针对Objective-C和Swift语言,帮助读者从零基础开始踏入iOS应用开发的世界。这本书以英文原版的形式提供,旨在通过实际项目驱动的...

    iOS_UIImageView and 手势

    在iOS开发中,`UIImageView`是用于展示图片的常用组件,而手势识别(Gesture Recognizers)则为用户交互提供了丰富的可能性。本篇文章将深入探讨`UIImageView`与手势API的结合使用,帮助开发者创建更具交互性的iOS...

Global site tag (gtag.js) - Google Analytics