简体   繁体   中英

Calling a func from one class in another class - swift

I have a class viewcontroller . In another class, SigViewController , I have a function which captures an image of a signature. I would like to call the function in the viewcontroller class (specifically in an IBAction).

In viewcontroller I have the following.

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

In SigViewController the function is...

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.

Thanks for any help.

getSignature() is an instance function so you need to make an instance of SigViewController class to call it. You can call class functions in another class using the syntax ClassName.FunctionName() if you mark the function as a class function. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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