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

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
hahaha
  • 1,379
  • 4
  • 11
  • 22
  • Pass self as AnyObject i.e self.getNewPhotoAction(self) – Saurabh Prajapati Nov 12 '14 at 04:57
  • @himanshu-padia Do not add blockquotes for things that are not code. And if you edit, don't forget to properly capitalize nouns and to fix errors. Just editing to add the language name in blockquotes is not acceptable. Thanks. – Eric Aya May 10 '16 at 12:59

8 Answers8

42

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")
}
linimin
  • 6,239
  • 2
  • 26
  • 31
  • Only do this as a last resort, IBActions are intended to be colled from the user interface code. Calling these directly is unexpected and will result in code that is difficult to follow and understand. – xpereta Jun 27 '18 at 14:45
5

You could always create a separate func that you call on in your viewDidLoad or in your IBAction

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

func getNewPhoto(){
    //do whatever you want here. 
    println("getnewphotoaction")
    println("whatever you want")
}
@IBAction func getNewPhotoAction(sender: AnyObject) {
    self.getNewPhoto()
}
Amerino
  • 389
  • 1
  • 5
4

Swift 4.2

@IBAction func getNewPhotoAction(sender: Any) {
    println("getNewPhotoAction")
}

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

If you still need to reference the UIButton or whatever is sending the action and want to call it from code at the same time - you can do this too:

onNext(UIButton())

Wasteful, but less code.

Alexandre G
  • 1,655
  • 20
  • 30
2

You actually don't need to pass any object at all. If you have no need to use the sender, then declare the function without it like this:

@IBAction func getNewPhotoAction() { ... }

And use it like this:

self.getNewPhotoAction()

You may need to re-connect the outlet in interface builder when you make this change (remove it and then add it back) if this method is connected to an event in interface builder.

Himanshu padia
  • 7,428
  • 1
  • 47
  • 45
jaime
  • 927
  • 8
  • 13
  • 1
    99% of the ibactions have no use for the sender. This both works around the swift mangling issue that went awry in xcode 8 AND makes code more compact. thank you! – Anton Tropashko Sep 28 '16 at 10:15
2
@IBAction func getNewPhotoAction(sender: AnyObject?){
    ......
}

**AnyObject** means that you have to pass kind of Object which you are using, nil is not a AnyObject.
But **AnyObject?**, that is to say AnyObject is Optional, nil is a valid value.
meaning the absence of a object.

self .getNewPhotoAction(nil)
Himanshu padia
  • 7,428
  • 1
  • 47
  • 45
1
@IBAction func getNewPhotoAction(sender: AnyObject? = nil) {
    print("getNewPhotoAction")
}

override func viewDidLoad() {
    super.viewDidLoad()
    self.getNewPhotoAction(nil)
}
米米米
  • 970
  • 7
  • 9
0

Since you don't have any sender, hand over nil.

self.getNewPhotoAction(nil)
David
  • 3,388
  • 2
  • 21
  • 25