iOS 对多个 ViewController 使用相同的 xib

标签 ios inheritance uiviewcontroller xib

我正在创建一个应用程序并开始遇到一个问题,即我的应用程序的大小变得非常大,我正在寻找可以减小大小的方法。我有 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/

相关文章:

ios - CollectionViewController,我应该使用普通的 UIViewController 子类化或实现它的委托(delegate)/数据源吗?

ios - 将 HTML 附加到 UIWebView

ios - 如何在 swift 中创建可变数量的 UIImageViews

python - 覆盖 Django Rest 中的 authToken View

Javascript继承思想?

ios - 从回调中调用 UiViewController

ios - UIPageViewController (ScrollStyle) 不调用 dataSource 方法

ios - Google Analytics iOS SDK,调度间隔很长

ios - 从 EDSunriseSet 获取夜间模式

postgresql - (如何)是否可以将表转换为 Postgres 中的外部表?