ios - 从其他 ViewController 调用方法 | swift

标签 ios swift view controller viewcontroller

我搜索了很多问题来解决这个问题,但没有人帮助我。 有 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/

相关文章:

wpf - 如果存储库在 View 模型中不可用,如何从绑定(bind)到 View 模型的 DetailView 中保存数据?

swift - 静默安装应用程序

ios - iOS 上的 VideoToolbox 示例?

iphone - 在 UILabel xcode 中显示 NSLog 结果

ios - swift IOS : Call Specific function of UIviewController when open app from notification

swift - Sprite Kit UIActivityIndi​​cator 不会停止动画

mysql - 在 MySQL 中创建一个 View ,其中包含从表中的行数据创建的列?

c# - MonoTouch.Dialog 搜索过滤器不适用于 MessageElement 单元格

ios - Xcode 6.4 : External libraries not getting imported properly

ios - 如何从resumeData的NSURLSessionResumeCurrentRequest添加HTTPHeaderField的值?