Ios 使用“继承CustomViewController的正确方法”;“依赖性”;
最近我编写了一个应用程序,其中只有一个场景和Ios 使用“继承CustomViewController的正确方法”;“依赖性”;,ios,swift,uiview,uiviewcontroller,Ios,Swift,Uiview,Uiviewcontroller,最近我编写了一个应用程序,其中只有一个场景和ViewController。我必须为视图设置自定义背景图片,由视图控制器管理(即,我的顶视图包含UIImageView)。后来,我不得不在ViewController中实现一些逻辑,以便它在屏幕旋转时正确地旋转/更改图片。此外,我还必须为ViewController覆盖一些属性,如preferredStatusBarStyle 现在我必须在我的应用程序中实现更多的场景/屏幕,结果证明它们都必须具有与当前屏幕相同的设计,因此我认为如果我创建一个Comm
ViewController
。我必须为视图
设置自定义背景图片,由视图控制器
管理(即,我的顶视图包含UIImageView
)。后来,我不得不在ViewController
中实现一些逻辑,以便它在屏幕旋转时正确地旋转/更改图片。此外,我还必须为ViewController
覆盖一些属性,如preferredStatusBarStyle
现在我必须在我的应用程序中实现更多的场景/屏幕,结果证明它们都必须具有与当前屏幕相同的设计,因此我认为如果我创建一个CommonViewController
,其中包含背景图片的公共旋转相关逻辑,这是有意义的,这样我就可以从这个CommonViewController
继承所有其他ViewController
。我唯一的问题是,CommonViewController
“要求”它管理的视图具有backgroundPicture:UIView
属性,我不知道如何确保该属性
如果我与XIB文件一起创建一个新文件CommonViewController
,我可以在XIB中添加backgroundPicture
图像视图,并将其与代码连接(通过常规的“控制拖动”方法),但显然这不起作用,因为不能保证继承CommonViewController
的视图将具有此属性。在Swift中,解决这个问题的正确方法是什么
不幸的是,我找不到解决办法,也许我一直在寻找错误的东西。似乎我需要为每个场景继承一个CommonViewController
(对于每个CustomViewController
),但我还必须以某种方式将每个控制器的俯视图设置为与一些CommonView
相等,这样当我尝试访问@IBOutlet弱var backgroundPicutre:UIImageView时,CommonViewController
不会崩溃代码>
显而易见的方法是在CommonViewController
中定义一些方法或属性,以便继承它的控制器可以实现/覆盖它,但这似乎有点老套,因为它仍然需要在继承CommonViewController
的每个ViewController
中进行复制粘贴
我如何想象解决方案:我创建CustomViewController:CommonViewController
,然后在我的故事板中创建一个视图控制器,并将“Class”属性更改为“CustomViewController”(在属性编辑器中),然后选择与此新添加的控制器对应的视图,并将“Class”属性更改为“BackgroundImageView。但我不确定这样做是否正确(我也怀疑
CustomViewController是否能正确“连接”它的
IBOutlet字段
bakcgroundImageView与
BackgroundImageView`中相应的
UIView,这就是为什么我想问专家们对它的看法。我认为你应该定义你的基本控制器(CommonViewController
)完全在代码中,即不使用基本控制器的XIB/序列图像板。这并不意味着您应该完全摆脱序列图像板/XIB。除CommonViewController
之外的所有其他视图控制器的接口仍然可以用XIB/序列图像板实现
在这种情况下,CommonViewController
实现可能如下所示:
import UIKit
class CommonViewController: UIViewController {
// use this property every time you need to
// manipulate backgroundPicture
var backgroundPicture: UIImageView = {
// Replace with your image name
let image = UIImage(named: "BackgroundPicture")!
let imageView = UIImageView()
imageView.image = image
return imageView
}()
override func viewDidLoad() {
// If subclass overrides viewDidLoad()
// it should contain super.viewDidLoad()
super.viewDidLoad()
view.addSubview(backgroundPicture)
// Align backgroundPicture to bounds of superview
// You can remove this code and implement
// your own alignment with frames or Autolayout
backgroundPicture.frame = view.bounds
// Send backgroundPicture to back of the view
// Otherwise backgroundPicture may overlap views added in subclasses
view.sendSubviewToBack(backgroundPicture)
}
override func viewDidLayoutSubviews() {
// If subclass overrides viewDidLayoutSubviews()
// It should contain super.viewDidLayoutSubviews()
super.viewDidLayoutSubvews()
// Align backgroundPicture to bounds of superview
// You can remove this code and implement
// your own alignment with frames or Autolayout
backgroundPicture.frame = view.bounds
}
}
谢谢你的想法;但是在你的方法中…这样做安全吗?self.view=commonView
?在这种情况下,我将无法使用CommonViewController
来管理任何其他视图,对吗?(因为它覆盖了loadView()
中的视图,并且将使用一些内部视图,而不是“真实”视图)视图它是用来管理的?我已经仔细阅读了,你是对的。如果控制器或它的子类使用nib文件,覆盖loadView是个坏主意。我更新了答案,这样它就不再使用loadView了