iOS - 如何在 Swift 中的不同视图控制器中使用小视图
iOS - How to use a small view in different view controllers in Swift
我有一个进度条(有自己的控制器)。该栏应该显示在不同的视图中,具体取决于哪个视图可见。由于进度相同,如果可能的话,我不想在多个视图中创建许多进度条,而是想在所有这些视图中使用相同的实例。同样,当我需要更改进度条的任何 属性 时,它也会以这种方式普遍反映出来,这是必需的。
请建议我如何使用这个通用视图。而且如果我的策略是错误的,对于这种情况,什么是更好的设计。
1) 那么你有 2 个选择。您可以声明一个新的 Class ViewBox
(或任何名称),然后在您的代码中使用它
第一个视图控制器
var box:ViewBox = ViewBox()
当您继续或转换到下一个屏幕时,您可以有一个预定义变量 var box:ViewBox!
。然后说当你按下一个按钮时,按钮有一个函数叫做transition.
//Now setup the transition inside the Storyboard and name the identifier "toThirdViewController"
override func prepareForSegue(segue:UIStoryboardSegue, sender:AnyObject?) {
if(segue.identifier == "toThirdViewController") {
var vc = segue.destinationViewController as! `nextViewController` //The class of your next viewcontroller goes here
vc.box = self.box
}
//Since The SecondViewController doesn't need ViewBox, we don't need it there.
}
其中
nextViewController:UIViewController {
var box:ViewBox!
}
或者你可以做一个更简单的方法,那就是查找 UIPageViewController
:)
我有一个进度条(有自己的控制器)。该栏应该显示在不同的视图中,具体取决于哪个视图可见。由于进度相同,如果可能的话,我不想在多个视图中创建许多进度条,而是想在所有这些视图中使用相同的实例。同样,当我需要更改进度条的任何 属性 时,它也会以这种方式普遍反映出来,这是必需的。
请建议我如何使用这个通用视图。而且如果我的策略是错误的,对于这种情况,什么是更好的设计。
1) 那么你有 2 个选择。您可以声明一个新的 Class ViewBox
(或任何名称),然后在您的代码中使用它
第一个视图控制器
var box:ViewBox = ViewBox()
当您继续或转换到下一个屏幕时,您可以有一个预定义变量 var box:ViewBox!
。然后说当你按下一个按钮时,按钮有一个函数叫做transition.
//Now setup the transition inside the Storyboard and name the identifier "toThirdViewController"
override func prepareForSegue(segue:UIStoryboardSegue, sender:AnyObject?) {
if(segue.identifier == "toThirdViewController") {
var vc = segue.destinationViewController as! `nextViewController` //The class of your next viewcontroller goes here
vc.box = self.box
}
//Since The SecondViewController doesn't need ViewBox, we don't need it there.
}
其中
nextViewController:UIViewController {
var box:ViewBox!
}
或者你可以做一个更简单的方法,那就是查找 UIPageViewController
:)