在多个视图控制器中重用从 nib 创建的 uiview

发布于 2024-10-11 17:49:46 字数 66 浏览 5 评论 0原文

好吧,抱歉,简而言之:在也从 nib 创建的多个视图控制器中重用从 nib 创建的视图的最佳/最简单/推荐方法是什么?

OK, sorry, In a nutshell: What is the best/easiest/recommended way of reusing a view created from a nib in multiple viewcontrollers that are also created from nibs?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十二 2024-10-18 17:49:46

使用 NSBundle- (NSArray *)loadNibNamed:(NSString *)name Owner:(id)owner options:(NSDictionary *)options 方法加载您的笔尖。

  • 创建一个 nib 文件并将文件的所有者设置为您的视图控制器之一。只要所有相关属性都存在于两者中,哪一个并不重要。
  • 设置笔尖,链接您的视图/按钮/等。文件的所有者属性。

在您的视图控制器中执行以下操作

@interface MyViewController : UIViewController {
}

@property (retain, nonatomic) IBOutlet UIView *myView;
// Add whatever other outlets you need for your nib.

@end

@implementation MyViewController

@synthesize myView;

- (void)viewDidLoad {
    [super viewDidLoad];

    // You set up your other views/ivars/etc. here

    [[NSBundle mainBundle] loadNibNamed:@"MyCustomView" owner:self options:nil];
    // Assuming that your bundle contains a single top-level object that is linked to the
    //  'myView' property in your view controller, everything should be properly retained
}

@end

Use the - (NSArray *)loadNibNamed:(NSString *)name owner:(id)owner options:(NSDictionary *)options method of NSBundle to load your nib.

  • Create a nib file and set the File's owner to one of your view controllers. It shouldn't matter which one as long as all the relevant properties are present in both.
  • Set up the nib, linking your views/buttons/etc. to the file's owner properties.

In your view controllers do the following

@interface MyViewController : UIViewController {
}

@property (retain, nonatomic) IBOutlet UIView *myView;
// Add whatever other outlets you need for your nib.

@end

@implementation MyViewController

@synthesize myView;

- (void)viewDidLoad {
    [super viewDidLoad];

    // You set up your other views/ivars/etc. here

    [[NSBundle mainBundle] loadNibNamed:@"MyCustomView" owner:self options:nil];
    // Assuming that your bundle contains a single top-level object that is linked to the
    //  'myView' property in your view controller, everything should be properly retained
}

@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文