如何在 Swift 中的单独 class 中使用函数时添加视图?
How to add views while using a function in a separate class in Swift?
我创建了一个带有 class 的新 Swift-file
,它包含一个函数,该函数应该向 viewcontroller
。
当此代码在 ViewController
中时它可以工作,但在单独的文件中我收到一些错误消息:
class InitialiseLabels {
func setupLabels() {
// multiple variables are declared without problems, but then:
let viewWidth = view.frame.width // Use of unresolved identifier 'view'
let viewHeight = view.frame.height // Use of unresolved identifier 'view'
view.addSubview(myLabel) // Use of unresolved identifier 'view'
// etcetera
myTextField.delegate = self as" UITextFieldDelegate // Use of unresolved identifier 'self'
view.addSubview(myTextField) // Use of unresolved identifier 'view'
}
}
我想我应该在视图前面添加一些东西来指定它应该去哪里(比如 self.view
但那个显然不起作用,这个 ViewController
的名称也不起作用'不工作)。有人可以帮我吗?
也许创建 swift 文件的单例,然后在调用将 UILabel、UIButton 等添加到当前视图控制器的函数时引用该单例
假设以下是 swift 文件
class ButtonCreator : NSObject {
//Declare the Singleton
static let shared = buttonCreator()
func thisFunctionCreatesandReturnAButton(withFrame: CGRect) -> UIButton {
let button = UIButton(frame: withFrame)
return button
}
}
现在您可以在整个应用程序(任何视图控制器)中引用它作为
self.view.addSubview(ButtonCreator.shared.thisFunctionCreatesandReturnAButton(withFrame:CGRect(0,0,100,44)))
创建一个 class MyView
并将其分配给 VC 的主视图。并将您的代码放入 class.
class MyView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
override func layoutSubviews() {
super.layoutSubviews()
// add code to align view properly
}
private setup() {
....
let viewWidth = frame.width // Use of unresolved identifier 'view'
let viewHeight = frame.height // Use of unresolved identifier 'view'
addSubview(myLabel)
myTextField.delegate = self as" UITextFieldDelegate // Use of unresolved identifier 'self'
addSubview(myTextField) /
...
}
}
您的问题 "How to add views while using a function in a separate class in Swift?" 的简短回答是 "Don't do that."
您应该将视图控制器的视图视为私有的。视图控制器应该对它们负责。
如果您真的想让外部对象向您的视图控制器添加视图(同样,您不应该这样做),您需要引用另一个视图控制器。创建一个将新视图作为参数并为调用者添加它们的函数会更清晰:
public func addViewdToContent(_ views: [UIView])
然后从另一个对象,你需要引用那个视图控制器:
let otherViewConroller = //code to somehow get the other view controller.
let contentView = otherViewConroller.view
let viewWidth = contentView.frame.width
let viewHeight = view.frame.height
otherViewConroller.addViewToContent([myLabel]
你的下一行没有任何意义,你得到的错误也没有意义。让其他对象成为视图控制器视图的委托是一个非常糟糕的主意。 也将需要委托的文本字段之类的视图注入另一个视图控制器是个坏主意。
myTextField.delegate = self as" UITextFieldDelegate // makes no sense
改变
class InitialiseLabels
到
extension ViewController
即使它在另一个文件中,它也必须是原始 ViewController class 的一部分,而不是其他 class。
我发现我做错了什么:我把函数的声明放在 viewdidload 中,因此它在视图控制器的其他部分不可用。因此我想把它放在一个单独的 class 或扩展中,但我现在意识到解决方案是将函数声明放在视图控制器 class 声明之后。
我创建了一个带有 class 的新 Swift-file
,它包含一个函数,该函数应该向 viewcontroller
。
当此代码在 ViewController
中时它可以工作,但在单独的文件中我收到一些错误消息:
class InitialiseLabels {
func setupLabels() {
// multiple variables are declared without problems, but then:
let viewWidth = view.frame.width // Use of unresolved identifier 'view'
let viewHeight = view.frame.height // Use of unresolved identifier 'view'
view.addSubview(myLabel) // Use of unresolved identifier 'view'
// etcetera
myTextField.delegate = self as" UITextFieldDelegate // Use of unresolved identifier 'self'
view.addSubview(myTextField) // Use of unresolved identifier 'view'
}
}
我想我应该在视图前面添加一些东西来指定它应该去哪里(比如 self.view
但那个显然不起作用,这个 ViewController
的名称也不起作用'不工作)。有人可以帮我吗?
也许创建 swift 文件的单例,然后在调用将 UILabel、UIButton 等添加到当前视图控制器的函数时引用该单例
假设以下是 swift 文件
class ButtonCreator : NSObject {
//Declare the Singleton
static let shared = buttonCreator()
func thisFunctionCreatesandReturnAButton(withFrame: CGRect) -> UIButton {
let button = UIButton(frame: withFrame)
return button
}
}
现在您可以在整个应用程序(任何视图控制器)中引用它作为
self.view.addSubview(ButtonCreator.shared.thisFunctionCreatesandReturnAButton(withFrame:CGRect(0,0,100,44)))
创建一个 class MyView
并将其分配给 VC 的主视图。并将您的代码放入 class.
class MyView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
override func layoutSubviews() {
super.layoutSubviews()
// add code to align view properly
}
private setup() {
....
let viewWidth = frame.width // Use of unresolved identifier 'view'
let viewHeight = frame.height // Use of unresolved identifier 'view'
addSubview(myLabel)
myTextField.delegate = self as" UITextFieldDelegate // Use of unresolved identifier 'self'
addSubview(myTextField) /
...
}
}
您的问题 "How to add views while using a function in a separate class in Swift?" 的简短回答是 "Don't do that."
您应该将视图控制器的视图视为私有的。视图控制器应该对它们负责。
如果您真的想让外部对象向您的视图控制器添加视图(同样,您不应该这样做),您需要引用另一个视图控制器。创建一个将新视图作为参数并为调用者添加它们的函数会更清晰:
public func addViewdToContent(_ views: [UIView])
然后从另一个对象,你需要引用那个视图控制器:
let otherViewConroller = //code to somehow get the other view controller.
let contentView = otherViewConroller.view
let viewWidth = contentView.frame.width
let viewHeight = view.frame.height
otherViewConroller.addViewToContent([myLabel]
你的下一行没有任何意义,你得到的错误也没有意义。让其他对象成为视图控制器视图的委托是一个非常糟糕的主意。 也将需要委托的文本字段之类的视图注入另一个视图控制器是个坏主意。
myTextField.delegate = self as" UITextFieldDelegate // makes no sense
改变
class InitialiseLabels
到
extension ViewController
即使它在另一个文件中,它也必须是原始 ViewController class 的一部分,而不是其他 class。
我发现我做错了什么:我把函数的声明放在 viewdidload 中,因此它在视图控制器的其他部分不可用。因此我想把它放在一个单独的 class 或扩展中,但我现在意识到解决方案是将函数声明放在视图控制器 class 声明之后。