我需要一个自定义的 UIView
,它可以在多个不同的 ViewControllers
中使用。
通常当我创建一个自定义的 UIView
时,我将 UIView
的一个实例拖到 Storyboard中的 ViewController
中,创建一个 UIView< 的子类
,并在 View 的 Identity Inspector
中指向这个子类。从那里,我会将 UI 对象作为导出连接到子类的头文件。
我现在要制作的 View 不应该是任何特定 Controller 的一部分,但应该添加到任何需要它的 Controller 中。一个流氓 UIView
。我知道我可以以编程方式创建整个 UIView
,并且只创建它的一个实例,但我想在我的 Storyboard中拥有它(并设计它)。
在 Storyboard 中,唯一允许我拖到“ViewController 之外”的对象是其他 ViewController。
除了 Storyboard 之外,我从未使用过其他任何东西来进行 iOS 开发,但我看过使用其他模式的教程,从过去,这看起来正是我需要的。是否有可能将类似的内容放入我的 Storyboard?还是这需要自己的设置/设计?如果是,怎么办?
我也在考虑通过添加一个包含我的自定义 View 的“幻影”UIViewController
来解决这个问题;在那里设计它,但从其他 Controller 实例化它,但这听起来很 hacky..
我也想用 UITableViewCell
来做到这一点,但我想这会有相同的解决方案。
最佳答案
对于您的 UIView,您应该在 Storyboard中创建自定义 UIViewController 并实例化 View Controller 以访问 View :
MyViewController *myViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"MyViewController"];
// Access the view using "myViewController.view"
自 Storyboard出现以来,这在 iOS 中是一种非常普遍的做法。如果你使用多个 Storyboard,你应该创建一个新的 UIStoryBoard 实例:
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"StoryboardName" bundle:nil];
然后用 Storyboard的这个实例实例化 View Controller 。
如果您只想使用 UIView,您应该创建一个 .xib 文件,即 过去 格式。要创建自定义 UITableViewCell,我绝对会使用 .xib 文件。您最后的选择是以编程方式创建一个 UIView,它可以从您应用程序的任何地方调用。根据此 View 的复杂性,这可能是一个有效的选项。
关于ios - 有没有办法在 Storyboard中将自定义 UIView 设置为 'design',并在不添加到任何特定 Controller 的情况下在多个 ViewController 中使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20370366/