从几个不同的UIViewControllers加载NIB文件中的视图 [英] Load view from NIB file from several different UIViewControllers

查看:153
本文介绍了从几个不同的UIViewControllers加载NIB文件中的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 site

[[NSBundle mainBundle] loadNibNamed:@"YourNibName" owner:self options:nil];

问题在于,因为我们必须设置文件所有者,所以此nib文件变为附加到这个视图控制器。这个视图是 UITableViewCell 的子类,我想从几个不同的vc加载这个nib文件。
感谢您的帮助。

The problem is that, because we have to set the File Owner, this nib file becomes "attached" to this view controller. This view is subclass of UITableViewCell and I wanted to load this nib file from several different vc's. Thanks for your help.

推荐答案

我将提供两种选择:


  • 创建一个类NibLoader,其中包含一个 @property(非原子,保留)IBOutlet id mainObject; 以及一个名为<$的方法C $ C> loadNibNamed:束:)。然后,执行 MyView * v = [[NibLoader loadNibNamed:MyViewbundle:nil] mainObject]; 。 (GCC 4.0属性访问错误意味着[...]。mainObject会调用[...]两次;它已在4.2中修复。)

  • 创建 @protocol MyNibOwner 其中包含 @property(非原子,保留)IBOutlet MyView * myView; ,并将文件的所有者类更改为 id< MyNibLoader> NSObject< MyNibLoader>

  • Create a class NibLoader with a single @property (nonatomic, retain) IBOutlet id mainObject; and a method called loadNibNamed:bundle:). Then, do MyView * v = [[NibLoader loadNibNamed:"MyView" bundle:nil] mainObject];. (A GCC 4.0 property access bug meant that [...].mainObject would call [...] twice; it's been fixed in 4.2.)
  • Create @protocol MyNibOwner which has @property (nonatomic, retain) IBOutlet MyView * myView;, and change the file's owner class to id<MyNibLoader> or NSObject<MyNibLoader>.

这篇关于从几个不同的UIViewControllers加载NIB文件中的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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