I don't do it often because it's troublesome to have other class instances with the information I need, but I just pass the other class instances instead of self
, so I just need to do the following:
myButton.addTarget(theInstanceOfOtherClass, action: "myFunction", forControlEvents:.TouchUpInside)
UIControl Class Reference
Naturally, theInstanceOfOtherClass
has generated an instance before calling this line, and the instance must remain for the duration of the action method being called.Normally, it would be the same ViewController instance property as having UIButton.
Then, the outline looks roughly like this.
class OtherClass {
@objc func myFunction(){
//...
}
}
classViewController:UIViewController {
@IBOutlet weak var myButton: UIButton!
var theInstanceOfOtherClass=OtherClass()
override func viewDidLoad(){
super.viewDidLoad()
// Do any additional setup after loading the view, typically from anib.
myButton.addTarget(theInstanceOfOtherClass, action: "myFunction", forControlEvents: .TouchUpInside)
//...
}
//...
}
I'm not sure what I really want to do, but in some cases it might be better to consider other methods such as using the delete pattern.
771 GDB gets version error when attempting to debug with the Presense SDK (IDE)
1235 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
758 Error in x, y, and format string must not be None
856 Uncaught (inpromise) Error on Electron: An object could not be cloned
© 2025 OneMinuteCode. All rights reserved.