`

[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

 

 

 

分享到:
评论

相关推荐

    【Linux设备管理】从devfs到udev:设备文件系统演变及其关键技术对比分析了Linux设备管理

    内容概要:本文详细介绍了Linux设备文件系统从devfs到udev的演变过程。devfs作为Linux 2.4时代的创新,通过引入内核空间的设备管理机制,简化了设备文件的创建和管理,但其存在稳定性问题和灵活性不足的缺点。udev则运行在用户空间,利用内核发送的热插拔事件和sysfs提供的信息,实现了设备文件的动态管理和高度自定义。它不仅解决了devfs的问题,还提供了更灵活、高效的设备管理方式,成为现代Linux系统中的主流选择。文章还探讨了两者在不同应用场景中的优劣,并展望了未来Linux设备管理的发展方向,强调了对新型设备的支持、虚拟化和容器环境的兼容性以及智能化的用户体验。 适合人群:对Linux系统有一定了解,特别是对设备管理感兴趣的开发人员、系统管理员和技术爱好者。 使用场景及目标:①理解Linux设备管理的历史和发展趋势;②掌握devfs和udev的工作原理及其在不同场景下的应用;③学习如何编写udev规则文件,实现设备的个性化管理和优化配置。 阅读建议:本文内容较为技术性,建议读者先了解基本的Linux设备管理概念。在阅读过程中,重点关注devfs和udev的区别和优势,并结合实际应用场景进行思考。对于udev规则文件的编写,可以通过实践和调试加深理解。

    三维路径规划中RRT与APF融合算法及其路径平滑处理

    内容概要:本文详细介绍了将快速随机树(RRT*)和人工势场(APF)相结合用于三维空间路径规划的方法。首先阐述了两种算法的工作原理,特别是APF如何通过引力和斥力向量引导RRT*的节点扩展,使得路径规划更加高效且能够有效避开障碍物。接着讨论了路径平滑处理的具体实现方式,如利用贝塞尔曲线进行路径优化,确保最终路径不仅平滑而且不会发生碰撞。此外,文中还提供了具体的代码片段来展示各个模块的功能,包括APF核心算法、RRT*扩展逻辑、碰撞检测以及路径平滑等。同时提到了一些潜在的改进方向,如引入速度场因素、采用不同的平滑算法等。 适合人群:对机器人路径规划、无人驾驶等领域感兴趣的开发者和技术爱好者。 使用场景及目标:适用于需要在复杂三维环境中进行高效路径规划的应用场合,如无人机飞行、自动驾驶汽车等。主要目的是提高路径规划的速度和质量,使生成的路径更加平滑、安全。 其他说明:本文不仅提供了理论解释,还有详细的代码实现,便于读者理解和实践。对于希望深入了解RRT*和APF融合算法并应用于实际项目的读者来说是非常有价值的参考资料。

    HikvisionIVMSGetShell-main.zip

    HikvisionIVMSGetShell-main.zip

    动态演示后缀表达式的计算方式

    动态演示后缀表达式的计算方式

    大学战队2021雷达站视觉算法源码.zip

    1、该资源内项目代码经过严格调试,下载即用确保可以运行! 2、该资源适合计算机相关专业(如计科、人工智能、大数据、数学、电子信息等)正在做课程设计、期末大作业和毕设项目的学生、或者相关技术学习者作为学习资料参考使用。 3、该资源包括全部源码,需要具备一定基础才能看懂并调试代码。 大学战队2021雷达站视觉算法源码.zip大学战队2021雷达站视觉算法源码.zip 大学战队2021雷达站视觉算法源码.zip大学战队2021雷达站视觉算法源码.zip 大学战队2021雷达站视觉算法源码.zip大学战队2021雷达站视觉算法源码.zip 大学战队2021雷达站视觉算法源码.zip大学战队2021雷达站视觉算法源码.zip 大学战队2021雷达站视觉算法源码.zip大学战队2021雷达站视觉算法源码.zip 大学战队2021雷达站视觉算法源码.zip大学战队2021雷达站视觉算法源码.zip 大学战队2021雷达站视觉算法源码.zip大学战队2021雷达站视觉算法源码.zip 大学战队2021雷达站视觉算法源码.zip大学战队2021雷达站视觉算法源码.zip 大学战队2021雷达站视觉算法源码.zip大学战队2021雷达站视觉算法源码.zip 大学战队2021雷达站视觉算法源码.zip大学战队2021雷达站视觉算法源码.zip 大学战队2021雷达站视觉算法源码.zip大学战队2021雷达站视觉算法源码.zip

    MATLAB用户界面设计.pptx

    MATLAB用户界面设计.pptx

    程序员面试题精选100题.pdf

    程序员面试题精选100题.pdf

    牵牛花铅笔素材儿童教学课件模板.pptx

    牵牛花铅笔素材儿童教学课件模板

    基于C++的rviz机械臂各类仿真+源码+项目文档(毕业设计&课程设计&项目开发)

    基于C++的rviz机械臂各类仿真+源码+项目文档,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用,详情见md文档 基于C++的rviz机械臂各类仿真+源码+项目文档,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用,详情见md文档~ 基于C++的rviz机械臂各类仿真+源码+项目文档,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用,详情见md文档 基于C++的rviz机械臂各类仿真+源码+项目文档,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用,详情见md文档 基于C++的rviz机械臂各类仿真+源码+项目文档,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用,详情见md文档 基于C++的rviz机械臂各类仿真+源码+项目文档,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用,详情见md文档

    三相电流型PWM整流Matlab仿真:电压外环与电流内环双闭环控制策略详解

    内容概要:本文详细介绍了三相电流型PWM整流的Matlab仿真过程,采用了电压外环和电流内环的双闭环控制策略。电压外环负责维持直流侧电压的稳定,通过PI调节器输出电流给定值;电流内环根据电压外环的给定值和实际检测到的三相电流,经过PI调节器产生PWM波来控制整流器的开关动作,实现对交流侧电流的精确控制。文中提供了详细的系统参数设置、PI调节器参数设定、主循环仿真代码以及坐标变换的具体实现方法。此外,还讨论了PWM调制、开关信号生成、仿真结果分析等内容,并附有说明文档和参考文献。 适合人群:从事电力电子、控制系统设计的研究人员和技术人员,尤其是对PWM整流器感兴趣的工程师。 使用场景及目标:适用于研究和开发三相电流型PWM整流器的人员,帮助他们理解和实现双闭环控制策略,提高系统的稳定性和效率。目标是在不同工况下实现直流侧电压的稳定和网侧电流的正弦化,达到单位功率因数运行。 其他说明:文中提供的代码和仿真模型已在MATLAB 2021b及以上版本测试通过,附带的说明文档包含了参数整定表、典型波形库和故障排查指南,有助于解决实际应用中的问题。

    五相永磁同步电机单相开路故障下的矢量容错控制技术解析

    内容概要:本文详细探讨了五相永磁同步电机(PMSM)在发生单相开路故障时的矢量容错控制方法。首先介绍了五相电机相较于三相电机的优势及其容错机制的基本原理。随后,文章深入讨论了故障发生时的电流重构策略,包括Clarke变换矩阵的修改、电流补偿系数的选择以及相位补偿的方法。此外,文中还涉及了矢量控制的具体实现,如矢量合成、SVPWM调制方式的调整、电流环整定的自适应算法等。最后,通过实测数据分析了不同故障条件下的系统表现,并提出了进一步的研究方向和技术改进措施。 适合人群:从事电机控制系统设计与开发的技术人员,尤其是关注五相永磁同步电机容错控制领域的研究人员。 使用场景及目标:适用于需要提高电机系统可靠性和容错能力的应用场合,如工业机器人、电动汽车等领域。主要目标是在单相开路故障情况下,确保电机能够继续稳定运行并尽可能减少性能损失。 其他说明:文章不仅提供了理论分析,还包括大量实际案例和代码片段,有助于读者更好地理解和应用相关技术。同时提醒读者,在进行容错控制设计时要考虑实际系统的非线性特性,避免因参数设置不当而导致系统不稳定。

    电力系统暂态分析中PSS对单机无穷大系统稳定性影响的Simulink仿真研究

    内容概要:本文详细探讨了在单机无穷大系统中加入电力系统稳定器(PSS)前后,系统在不同扰动条件下的暂态响应情况。首先介绍了同步发电机的基本参数配置及其连接方式,然后分别进行了无PSS和带有PSS两种情况下系统的稳态运行、小扰动以及三相短路故障仿真实验。结果显示,PSS能够显著提高系统的阻尼水平,有效抑制因各种原因引起的振荡现象,确保系统快速恢复稳定状态。 适用人群:从事电力系统分析、自动化控制领域的研究人员和技术人员。 使用场景及目标:适用于希望深入了解PSS在电力系统中作用机制的研究者,以及需要评估PSS对于特定应用场景下系统性能改善效果的专业人士。通过本案例的学习,可以帮助使用者掌握如何利用MATLAB/Simulink进行相关仿真实验的方法。 其他说明:文中提供了详细的建模步骤和参数设定指南,并附有多张对比图表帮助理解PSS的作用效果。同时提醒了一些常见的操作误区,如仿真步长的选择等。

    2023-04-06-项目笔记 - 第四百七十三阶段 - 4.4.2.471全局变量的作用域-471 -2025.04-19

    2023-04-06-项目笔记-第四百七十三阶段-课前小分享_小分享1.坚持提交gitee 小分享2.作业中提交代码 小分享3.写代码注意代码风格 4.3.1变量的使用 4.4变量的作用域与生命周期 4.4.1局部变量的作用域 4.4.2全局变量的作用域 4.4.2.1全局变量的作用域_1 4.4.2.471局变量的作用域_471- 2025-04-19

    病理分割-基于深度学习实现的腹部多器官分割算法-附项目源码-优质项目实战.zip

    病理分割_基于深度学习实现的腹部多器官分割算法_附项目源码_优质项目实战

    基于三菱PLC和触摸屏的停车场智能管理系统设计与实现

    内容概要:本文详细介绍了基于三菱PLC和三菱触摸屏构建的停车场智能管理系统。系统分为入口、出口和管理中心三大部分,分别负责车辆身份识别、车位检测、道闸控制、缴费结算等功能。三菱PLC作为核心控制器,通过梯形图编程实现了车辆检测、道闸控制等关键逻辑;三菱触摸屏提供人机交互界面,支持参数设置、状态监控等功能。文中还讨论了PLC与触摸屏之间的通信配置,以及如何通过物联网技术将系统接入云端。 适合人群:从事智能交通系统开发的技术人员,尤其是熟悉三菱PLC编程和触摸屏应用的工程师。 使用场景及目标:适用于新建或改造停车场项目,旨在提高停车场管理效率和服务质量,减少人工干预,实现智能化运营。 其他说明:文中提供了具体的硬件配置建议、PLC编程实例、触摸屏界面设计指南及通信协议解析,有助于读者快速理解和实施类似项目。

    Code_20250419.html

    Code_20250419.html

    基于MSP430F5529的无线温湿度传输系统设计与实现

    内容概要:本文详细介绍了基于MSP430F5529微控制器构建的无线温湿度传输系统的完整设计方案。系统集成了DHT11湿度传感器、DS18B20温度传感器、Nokia5110显示屏、按键模块、报警模块和nRF24L01无线传输模块。文章不仅涵盖了硬件连接细节,还深入讲解了各模块的驱动代码实现,包括传感器数据读取、数据显示、按键处理、报警机制和无线数据传输。此外,还讨论了一些优化措施,如低功耗模式的应用和传感器供电稳定性。 适合人群:具有一定嵌入式系统开发基础的技术人员,尤其是对MSP430系列微控制器感兴趣的开发者。 使用场景及目标:适用于需要实时监测环境温湿度并进行远程数据传输的场合,如智能家居、农业温室、工业环境监测等。目标是帮助读者掌握MSP430F5529的实际应用技能,理解各模块之间的协同工作原理。 其他说明:文中提供的代码片段可以直接应用于实际项目中,有助于快速搭建原型系统。同时,文章还提供了一些调试经验和常见问题解决方案,便于读者在实践中少走弯路。

    台达DVP PLC RS485通信实现多台变频器频率读写及启停控制

    内容概要:本文详细介绍了如何利用台达DVP PLC通过RS485接口与多台变频器进行通信,实现读取设定频率、设定变频器频率及控制启动和停止的功能。主要内容涵盖硬件连接、通讯参数设置、编程实例及常见问题解决方案。文中提供了具体的梯形图代码示例,解释了MODBUS RTU协议的应用,包括功能码的选择、寄存器地址的定义、CRC校验的处理方法等。 适合人群:从事工业自动化领域的工程师和技术人员,特别是那些需要掌握PLC与变频器通信技能的人。 使用场景及目标:适用于需要集成PLC控制系统并管理多个变频器的工作环境,如工厂生产线、自动化设备等。目标是提高系统的稳定性和可靠性,确保各个变频器能够按照预设的要求高效运作。 其他说明:文中强调了实际应用中的注意事项,如硬件连接的准确性、通讯参数的一致性、数据传输的安全性等,并给出了详细的调试建议和优化措施。此外,还提到了一些常见的陷阱及其应对策略,帮助读者更好地理解和解决问题。

    MATLAB/Simulink中二阶线性自抗扰控制器(LADRC)的高效电机控制仿真模型

    内容概要:本文详细介绍了基于MATLAB/Simulink平台构建的二阶线性自抗扰控制器(LADRC)用于电机控制的应用。文章首先指出了传统PI控制器存在的响应慢、易振荡的问题,随后深入探讨了LADRC的核心组成部分:跟踪微分器TD、线性扩张状态观测器LESO以及误差反馈控制律。文中展示了LADRC在面对负载突变时表现出色的抗扰能力和快速响应特性,并提供了具体的参数调整方法和调试技巧。此外,作者还分享了将LADRC应用于实际工程项目的经验,强调了其相较于传统PI控制器的优势,特别是在鲁棒性和适应不同电机型号方面的表现。 适合人群:从事电机控制领域的工程师和技术人员,尤其是那些希望提高控制系统性能并解决传统PI控制器局限性的专业人士。 使用场景及目标:适用于需要快速响应和强抗扰能力的电机控制系统设计,旨在替代传统的PI控制器,以获得更好的动态响应和平滑的操作体验。具体应用场景包括但不限于伺服压机、绕线机等工业自动化设备。 其他说明:文中提到的所有代码片段均可以在MATLAB环境中运行,且已封装成Simulink模块,便于集成到现有系统中进行测试和验证。对于初次接触LADRC的新手而言,文中提供的调试经验和参数选择建议尤为宝贵。

Global site tag (gtag.js) - Google Analytics