如何根据nib文件创建UINavigationItem.TitleView? [英] How to create a UINavigationItem.TitleView based upon a nib file?

查看:90
本文介绍了如何根据nib文件创建UINavigationItem.TitleView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于UINavigationController的屏幕序列。在其中一个屏幕上,我想用自定义TitleView替换标准标题。自定义视图将使用nib文件创建,并将有一个图像和几个标签。

I have a UINavigationController bassed sequence of screens. On one of the screens I want to replace the standard title with a custom TitleView. The custom view will be created with a nib file and will have an image and several labels.

我是否创建了一个带有相关nib文件的UIViewController,加载了nib,以及将.view属性分配给TitleView?基本上,我如何使用一个笔尖,我可以布局必要的元素,然后将结果视图分配给TitleView属性?

Do I create a UIViewController w/ associated nib file, load the nib, and assign the .view property to the TitleView? Basically, how do I use a nib where I can layout the necessary elements and then assign the resulting view to the TitleView property?

推荐答案

考虑使用UINib - 它在iOS 4.0中公开。因为它为你缓存笔尖所以它会加倍。

Consider using UINib - it was exposed in iOS 4.0. It double perf since it caches the nib for you.

UINib *nib = [UINib nibWithNibName:@"TestView" bundle:nil];
UIView *myView = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0]];

objectAtIndex是指NIB中的顶级视图 - 通常是一个(索引0)但是如果有很多,你需要提供索引。

The objectAtIndex refers to the top level views in the NIB - typically there's one (index 0) but if there's many, you need to provide the index.

一旦你有了视图,你可以将它分配给navigationItem titleView

Once you have the view you can assign it to the navigationItem titleView

self.navigationItem.titleView = myView;
[myview release];

编辑:

如果你需要进入NIB中的各个控件,将标记放在它们上并通过viewWithTag访问它们。见这里:

If you need to get to the individual controls within the NIB, puts tags on them and access them via viewWithTag. See here:

http://useyourloaf.com/blog/2011/2/28/speeding-up-table-view-cell-loading-with-uinib.html

这篇关于如何根据nib文件创建UINavigationItem.TitleView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆