Swift - call @IBAction method in viewDidLoad without parameter

hahaha picture hahaha · Oct 13, 2014 · Viewed 19.4k times · Source
@IBAction func getNewPhotoAction(sender: AnyObject) {
    println("getNewPhotoAction")
}

override func viewDidLoad() {
    super.viewDidLoad()
    self.getNewPhotoAction(sender: AnyObject) // Error
}

I just want to call the getNewPhotoAction IBAction method in viewDidLoad.

Which parameter to enter in this line -> self.getNewPhotoAction(?????) ?

I don't have any parameter. I just need to call.

I used in Objective-C style:

[self getNewPhotoAction:nil]

but I don't know Swift style.

Answer

ylin0x81 picture ylin0x81 · Nov 12, 2014

The parameter sender indicates who are calling the action method. When calling from viewDidLoad, just pass self to it.

override func viewDidLoad() {
    super.viewDidLoad()
    getNewPhotoAction(self)
}

By the way, if the sender parameter of the getNewPhotoAction method wasn’t used, the parameter name can be omitted.

@IBAction func getNewPhotoAction(AnyObject) {
    println("getNewPhotoAction")
}