我搜索了很多问题来解决这个问题,但没有人帮助我。 有 2 个 viewController,我需要将数组从一个发送到另一个。
在第一个viewController中调用方法:
SubmitViewController.acceptContent(content)
接受其他:
var contentAccepted = [String]()
class func acceptContent(content: [String]){
contentAccepted = content
}
问题出在 contentAccepted 行上的错误:Instance member cannot be used on type UIViewController
最佳答案
你在你的类型方法中指的是 self 。 以下是苹果对此事的看法:
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
}
确保您确实需要类型方法。
关于ios - 从其他 ViewController 调用方法 | swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37439973/