Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - call @IBAction method in viewDidLoad without parameter

@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.

like image 569
hahaha Avatar asked Oct 13 '14 21:10

hahaha


Video Answer


1 Answers

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")
}
like image 166
ylin0x81 Avatar answered Sep 20 '22 18:09

ylin0x81