3
@IBAction func first(sender: AnyObject) {
    println("Hello World")
}

@IBAction func second(sender: Anyobject) {
// I need to call function first here. 
}

I need to use one function inside another, because the sender type is Anyobject, I don't know how to call it.

4
  • Why do you want to call an IBAction from another IBAction? Commented May 8, 2015 at 2:51
  • The code in the first function is actually complicated, it would be more convenient if I can call the second directly from inside the first Commented May 8, 2015 at 2:54
  • Yogesh, unfortunately it doesn't work. Commented May 8, 2015 at 2:58
  • FYI - you can make normal functions (using func) that are not bound to an IBAction. You will want to use an IBAction for firing button presses only. Commented May 8, 2015 at 3:33

2 Answers 2

4

Have you tried

func first(){
    println("Hello World")
}

@IBAction func second(sender: AnyObject) {
    first()
}
Sign up to request clarification or add additional context in comments.

Comments

1

If you are sure about the type of control,it should be UIControl object likeUIButton,UISegmentControl etc, then change the sender to that type.

If both function's parameter are same you can do like

@IBAction func second(sender: UIButton) {//it sure that you are clikcing UIButton
    self.first(sender)//if here also a button, else create the control and pass.
}

But why you want first function as @IBAction? If it is not attached to any control please make it as normal function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.