来自其他 ViewController 的调用方法 | Swift
Call method from other ViewController | Swift
我搜索了很多问题来解决这个问题,但没有人帮助我。
有 2 个 viewController,我需要将数组从一个发送到另一个。
第一个调用方法viewController:
SubmitViewController.acceptContent(content)
接受其他:
var contentAccepted = [String]()
class func acceptContent(content: [String]){
contentAccepted = content
}
问题出在 contentAccepted 行上的错误:实例成员不能用于类型 UIViewController
您在您的类型方法中指的是自我。
以下是苹果对此事的看法:
Within the body of a type method, the implicit self property refers to
the type itself, rather than an instance of that type. For structures
and enumerations, this means that you can use self to disambiguate
between type properties and type method parameters, just as you do for
instance properties and instance method parameters.
More generally, any unqualified method and property names that you use
within the body of a type method will refer to other type-level
methods and properties. A type method can call another type method
with the other method’s name, without needing to prefix it with the
type name. Similarly, type methods on structures and enumerations can
access type properties by using the type property’s name without a
type name prefix.
More info on type-methods (class-methods) can be found here
尝试这样做:
var contentAccepted = [String]()
class func acceptContent(content: [String]){
ViewController().contentAccepted = content
}
确保您确实需要类型方法。
您 运行 遇到的问题是 contentAccepted
是一个实例变量(应该是),而 acceptContent()
是一个 class 方法。因此,您的方法无法访问您的变量。在您能够访问它的变量之前,您需要创建一个 SubmitViewController
的实例。
let submitViewController = SubmitViewController()
submitViewController.contentAccepted = content
通过首先创建一个 SubmitViewController 实例,您现在可以访问该实例变量。
如果您在 segue 中将信息从一个视图控制器传递到另一个视图控制器,您可以考虑使用 prepareForSegue
。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "Submit" {
if let view = segue.destinationViewController as? SubmitViewController {
view.contentAccepted = content // If content is an instance variable
}
}
}
我搜索了很多问题来解决这个问题,但没有人帮助我。 有 2 个 viewController,我需要将数组从一个发送到另一个。
第一个调用方法viewController:
SubmitViewController.acceptContent(content)
接受其他:
var contentAccepted = [String]()
class func acceptContent(content: [String]){
contentAccepted = content
}
问题出在 contentAccepted 行上的错误:实例成员不能用于类型 UIViewController
您在您的类型方法中指的是自我。 以下是苹果对此事的看法:
Within the body of a type method, the implicit self property refers to the type itself, rather than an instance of that type. For structures and enumerations, this means that you can use self to disambiguate between type properties and type method parameters, just as you do for instance properties and instance method parameters.
More generally, any unqualified method and property names that you use within the body of a type method will refer to other type-level methods and properties. A type method can call another type method with the other method’s name, without needing to prefix it with the type name. Similarly, type methods on structures and enumerations can access type properties by using the type property’s name without a type name prefix.
More info on type-methods (class-methods) can be found here
尝试这样做:
var contentAccepted = [String]()
class func acceptContent(content: [String]){
ViewController().contentAccepted = content
}
确保您确实需要类型方法。
您 运行 遇到的问题是 contentAccepted
是一个实例变量(应该是),而 acceptContent()
是一个 class 方法。因此,您的方法无法访问您的变量。在您能够访问它的变量之前,您需要创建一个 SubmitViewController
的实例。
let submitViewController = SubmitViewController()
submitViewController.contentAccepted = content
通过首先创建一个 SubmitViewController 实例,您现在可以访问该实例变量。
如果您在 segue 中将信息从一个视图控制器传递到另一个视图控制器,您可以考虑使用 prepareForSegue
。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "Submit" {
if let view = segue.destinationViewController as? SubmitViewController {
view.contentAccepted = content // If content is an instance variable
}
}
}