简体   繁体   English

从另一个班级的另一个班级调用func-Swift

[英]Calling a func from one class in another class - swift

I have a class viewcontroller . 我有一个类viewcontroller In another class, SigViewController , I have a function which captures an image of a signature. 在另一个类SigViewController ,我有一个捕获签名图像的函数。 I would like to call the function in the viewcontroller class (specifically in an IBAction). 我想在viewcontroller类中调用该函数(特别是在IBAction中)。

In viewcontroller I have the following. viewcontroller我有以下内容。

@IBAction func sigsave(sender: AnyObject) {
    SigViewController().getSignature()
}

In SigViewController the function is... SigViewController ,功能是...

func getSignature() ->UIImage {
    UIGraphicsBeginImageContext(CGSizeMake(self.bounds.size.width,
        self.bounds.size.height))
    self.layer.renderInContext(UIGraphicsGetCurrentContext())
    var signature: UIImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    println("saved image")
    return signature
}

The viewcontroller is returning the error `Use of unresolved identifier SigViewController. viewcontroller返回错误`使用未解析的标识符SigViewController。

Thanks for any help. 谢谢你的帮助。

getSignature() is an instance function so you need to make an instance of SigViewController class to call it. getSignature()是一个实例函数,因此您需要创建SigViewController类的实例才能调用它。 You can call class functions in another class using the syntax ClassName.FunctionName() if you mark the function as a class function. 如果将函数标记为类函数,则可以使用语法ClassName.FunctionName()在另一个类中调用类函数。 But when you do that you don't have access to any non class level instance variables. 但是当您这样做时,您将无权访问任何非类级别的实例变量。

Regular functions are like - methods in Objective-C, class functions are like + functions. 常规函数就像-Objective-C中的方法,类函数就像+函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM