gpt4 book ai didi

iOS 对多个 ViewController 使用相同的 xib

转载 作者:行者123 更新时间:2023-11-29 10:33:51 24 4
gpt4 key购买 nike

我正在创建一个应用程序并开始遇到一个问题,即我的应用程序的大小变得非常大,我正在寻找可以减小大小的方法。我有 50 多个 xib,所以我认为这是我可以改进的领域之一。我有很多 xib,它们都与不同的 View Controller eacbh 相匹配,其中一些非常相似。

我希望能够采用这些类似的 ViewController,并为所有这些 ViewController 重用一个 xib 以减少尺寸。

我认为继承是实现此目的的最佳方式。创建我所有其他 ViewController 扩展的“SuperViewController”。 SuperViewController 将连接到 xib,然后我可以重用我需要的东西。

这是我在 SuperViewController.m 中的内容:

 @property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *emptyView;
@property (weak, nonatomic) IBOutlet UIButton *currentButton;
@property (weak, nonatomic) IBOutlet UIButton *quitWalkButton;

所有这些 IBOutlet 都在我的 xib 文件中,并且在我所有的 ViewController 中。每个 ViewController 只是做一些略有不同的事情。

问题是,因为它们在我的 .m 文件中,所以它们本质上是私有(private)的,我无法从子类访问它们。我可以将它们放在 .h 文件中并能够访问它们,但我不希望它们公开,我希望它们“受到保护”。此外,我不确定点击等操作将如何工作,因为这些操作始终位于 .m 文件中。

我开始认为继承不是在多个 View Controller 中重用 xib 文件的方法,但我不确定执行此操作的公认方法是什么。我们将不胜感激任何建议。

最佳答案

我会将这些导出保留在它们位于父类(super class)的 .m 文件顶部的位置,并将每个导出的第二个访问器放在单独的 header /扩展名中。

   //myViewControllerSuperclass+privateOutlets
# import "myViewControllerSuperclass"
@interface myViewControllerSuperclass(privateOutlets)

//this header to be imported to your subclasses
@property (readonly) UITableView *tableView_;
@property (readonly) UIView *emptyView_;
@property (readonly) UIButton *currentButton_;
@property (readonly) UIButton *quitWalkButton_;
//note trailing underscore..



//myViewControllerSuperclass.m
# import "myViewControllerSuperclass+privateOutlets"
@implementation myViewControllerSuperclass

#pragma mark - privateOutlets
-(UITableView *)tableView_{
return self.tableView;
}
-(UIView *)emptyView_{
return self.emptyView;
}
-(UIButton *)currentButton_{
return self.currentButton;
}
-(UIButton *)quitWalkButton_{
return self.quitWalkButton;
}
//these 'getters' just point to the existing outlets..

关于iOS 对多个 ViewController 使用相同的 xib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28102821/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com